Search code examples
ggplot2outline

R: Is it possible to render more precise outline weights in ggplot2?


Create a simple stacked bar chart:

require(ggplot2)
g <- ggplot(mpg, aes(class)) + 
geom_bar(aes(fill = drv, size=ifelse(drv=="4",2,1)),color="black",width=.5)

I can change the size values in size=ifelse(drv=="4",2,1) to lots of different values and I still get the same two line weights. Is there a way around this? (2,1) produces the same chart as (1.1,1) and (10,1). Ideally the thicker weight should be just a bit thicker than the standard outline, rather than ~10x bigger.

As added background, you can set size outside of aes() and have outline width scale as you'd expect, but I can't can't assign the ifelse condition outside of aes() without getting an error.

Sample Plot


Solution

  • The two size values you give as aesthetics are just just arbitrary levels that are not taken at face value by ggplot.

    You can use

    ggplot(mpg, aes(class)) +  
      geom_bar(
        aes(fill = drv, size = drv == "4"),
        color = "black", width = .5) + 
      scale_size_manual(values = c(1, 2))
    

    Where the values parameter allows you to specify the precise sizes for each level you define in the asesthetics.