-1

I am making a fairly complicated ggplot that consists of a time-series plot based on the left y axis over a double bar graph based on the right y axis. The plot looks good, but the legend denotes the colors of the different lines and bars with a solid black box that has that object's color around the margins of the black box. Is there any way to make the box the solid color instead of just an outline?

Here is my plot:

fullPlot<- ggplot() +
 geom_point(data = df,aes(x=datetime,y=VWC,color = Legend)) +
 geom_col(data = df, aes(x = datetime, y = cm/ab, color = Legend), width=1) +
 scale_color_discrete(name = "Legend", labels = c("Irrigation (cm)", "Precipitation (cm)", "Deep (30-60 cm)", "Shallow (0-30 cm)")) +
 theme(plot.title = element_text(hjust = 0.5)) +
 xlab("Date") +
 ylab("Volumetric Water Conent") +
 facet_wrap(~Treatment, nrow = 5, ncol = 1) +
 scale_x_datetime(breaks = date_breaks("2 days"), date_labels = "%b %d") +
 scale_y_continuous(sec.axis = sec_axis(~. *ab)) +
 theme(axis.text.x = element_text(angle = 45, hjust = 1))

Because it is based on two large datasets, I'm not sure if it would be helpful to include the data. If anyone would like that, I can add that in as well.

Please let me know if you have any solutions or if I should change my formatting here in any way. Thanks!

Tom
  • 377
  • 3
  • 13
  • Use fill= legend instead of color = legend -probably in geom_point. – mgriebe Jan 31 '20 at 17:40
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. Without any of your data, we can't run your code, and without being able to see any output, we don't know what your charts look like or how exactly you're trying to change them – camille Jan 31 '20 at 19:13

2 Answers2

1

Some data:

LEG=c("Irrigation (cm)", "Precipitation (cm)", 
"Deep (30-60 cm)", "Shallow (0-30 cm)")
df = data.frame(datetime=1:40,VWC=runif(40),
cm_ab = runif(40),Legend=rep(LEG,each=10))
df$Legend = factor(df$Legend,levels=LEG)

I guess your plot looks like this? plotting with your commands:

ggplot() +
 geom_point(data = df,aes(x=datetime,y=VWC,color = Legend)) +
 geom_col(data = df, aes(x = datetime, y = cm_ab, color = Legend), width=1) +
 scale_color_discrete(name = "Legend", labels = c("Irrigation (cm)", "Precipitation (cm)", "Deep (30-60 cm)", "Shallow (0-30 cm)")) +
 theme(plot.title = element_text(hjust = 0.5)) 

enter image description here

So try the following, you need to use fill, and to get a common legend, you need to specify scale_fill_discrete

ggplot() +
 geom_point(data = df,aes(x=datetime,y=VWC,color = Legend)) +
 geom_col(data = df, aes(x = datetime, y = cm_ab, fill = Legend), width=1,col="black") +
 scale_color_discrete(name = "Legend", labels = c("Irrigation (cm)", "Precipitation (cm)", "Deep (30-60 cm)", "Shallow (0-30 cm)")) +
 theme(plot.title = element_text(hjust = 0.5)) +
 scale_fill_discrete(name = "Legend", labels = c("Irrigation (cm)", "Precipitation (cm)", "Deep (30-60 cm)", "Shallow (0-30 cm)")) +
 theme(plot.title = element_text(hjust = 0.5))

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
0

See What is the difference between the color and fill argument in ggplot2?. As @mgriebe points out, you are using color, which is referred to the outline of the shape, as opposed to fill, which deals with the filling's color of the shape.

So, in your example, just replace color with fill where it is needed.

David Jorquera
  • 2,046
  • 12
  • 35
  • Thanks for sharing that! Strangely, when I replace `color` with `fill`, it solves the legend issue, but completely removes the bar graph data and changes both of the lines from the time series data to black. Pretty weird, any idea what's going on? – Tom Jan 31 '20 at 17:50
  • It would be very difficult without sample data. You don't have to share all of it; see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example to check how to `dput()` your data. – David Jorquera Jan 31 '20 at 17:52