0

I have the following code:

drawCombinedSeries <- function(data, xData, yData, dataGroup, title, fileName, outputPath) {
  
  plt <- ggplot(data, aes(x = xData, y = yData, fill = dataGroup)) +
    stat_summary(geom = "line", size=1, fun = mean, aes(color=dataGroup, group = dataGroup)) +
    stat_summary(geom = "ribbon", fun.data = mean_se, alpha = 0.3, aes(fill = dataGroup)) +
    labs(title = title, x="Month", y="") +
    theme(legend.position="bottom", axis.title.x = element_blank(), text = element_text(size=12, colour="black")) +
    scale_x_discrete(name = "Month", limits=c(1:12), expand = c(0,0)) 
  
  setwd(outputPath)
  ggsave(filename=fileName, width=10, height = 2.5)
  return(plt)
}

This function returns this plot:

Activity plot

What I desire is to change the colors of the lines to a grayscale, and rename the legend.

I tried to use theme_bw(), but it doesn't work because I already have my theme(). Regarding the legend, when I try to create my own legend, it only creates one for the last stat_summary, which is the ribbon, thus having two legends. I have looked for a way to deal with two stat_summary functions, because I think that's the issue, but I haven't found anything enlightening.

  • It's always nice to add a little bit of data to play with :-) And: how did you try to create the legend? – TobiO Oct 25 '21 at 15:50
  • 3
    The colors here are nothing to do with `theme` - they are the `color` and `fill` aesthetics. Try adding the line `scale_fill_grey() + scale_color_grey() +` into your plot - that should do the trick. – Allan Cameron Oct 25 '21 at 15:56
  • @AllanCameron That's right, I used only one of them at a time. The solution was to use both of them. Thank you! – RastaDeveloper Oct 26 '21 at 09:18
  • @TobiO The legend has been created automatically with the ```color/group``` field aesthetics of ```stat_summary```. I don't know how to attach the dataframe I'm using (I'm a newbie in stack overflow). However, the data I'm using is the activity of some github repositories and has 4 columns: the name of the repo, the amount of activity, the index(which is the month, x-axis), and the status, which is the dataGroup of the legend. I want to rename the legend title, but I haven't found a way. – RastaDeveloper Oct 26 '21 at 09:23
  • 1
    have a look here @RastaDeveloper :-) https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and hello :wave: :-) – TobiO Oct 26 '21 at 12:32

1 Answers1

0

I solved the problem of the legend name using the labs function changing the colorand fillparameters. In the plot, I have the color (which is the line) and the fill (which is the ribbon), thus having to change both to rename the legend.

I attach the working code.

drawCombinedSeries <- function(data, xData, yData, dataGroup, title, fileName, outputPath) {
  
  plt <- ggplot(data, aes(x = xData, y = yData, fill = dataGroup)) +
    stat_summary(geom = "line", size=.6, fun = mean, aes(color=dataGroup, group = dataGroup)) +
    stat_summary(geom = "ribbon", fun.data = mean_se, alpha = 0.25, aes(fill = dataGroup)) +
    labs(title = title, x="Month", y="", fill = "Status", color = "Status") +
    theme(legend.position="bottom", axis.title.x = element_blank(), text = element_text(size=12, colour="black"), panel.background = element_blank(), panel.grid = element_line(colour = "gray")) +
    scale_x_discrete(name = "Month", limits=c(1:12), expand = c(0,0)) +
    scale_fill_manual(values = c('Alive' = "#666666",'Zombie' = "#777777", 'Dead' = "#ffffff")) +
    scale_color_manual(values = c('Alive' = "#666666",'Zombie' = "#777777", 'Dead' = "#ffffff"))
  
  setwd(outputPath)
  ggsave(filename=fileName, width=10, height = 2.5)
  return(plt)
} 

I have used the scale_color_manualand scale_fill_manual to have a better visualization on the overlapping of the ribbons. To rename the legend, I have changed the fill and color field of labs() function.