1

For the geom_line in the ggplot below, I would like to have a straight green line in each region as all the dimension in the region has the same value.

Is there an easy way to do this? I am using the following code.

p<- ggplot(df, aes(y=value, x=country, fill=category)) + 
  geom_bar(stat="identity", position ="dodge")+
  geom_line(aes(y=value, group = NA), col="darkgreen")+
  xlab("regions")+ylab("Categories")+
  theme_bw()+
  theme(legend.position = "right")+
  scale_fill_manual(" ", values = c("Ne" = "#00AFBB", "Np" = "#FC4E07", "Nt" ="#4E84C4", "Ns" ="#E7B800"))+
  theme(legend.title=element_blank())+
  theme(legend.spacing.x = unit(0.3, 'cm'))+
  scale_y_continuous(expand = c(0, 0), limits = c(0, 100),breaks = c(0,10,20,30,40,50,60,70,80,90,100))+
  scale_color_manual(name="", values = c("mean"="#3ADF00"), labels = c("Average"))

Additionally, how can I add the x-axis labels to be in two rows so the labels do not overlap each other?

tropicalbath
  • 121
  • 1
  • 1
  • 12
  • Why do you have group set to NA for the line? Have you tried setting it to a dummy value? And why do you have two seemingly-similar `geom_line` calls? Beyond that, hard to do more than guess without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – camille Aug 26 '19 at 16:52
  • Two geom_line was just a mistake, I will try to set the dummy value for the line. Thank you! – tropicalbath Aug 26 '19 at 17:11
  • This is basically a [duplicate](https://stackoverflow.com/questions/41569793/adding-group-mean-lines-to-geom-bar-plot-and-including-in-legend). – Rui Barradas Aug 26 '19 at 17:25

1 Answers1

1

The solution is to use geom_errorbar, like in this possible duplicate.

library(ggplot2)

p <- ggplot(new_africa, aes(x = Africa, y = value, fill = dimension)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymax = groupMeans, ymin = groupMeans, colour = mean)) +
  scale_fill_manual(values = c(GEO = "#00AFBB", 
                               NC = "#FC4E07", 
                               RE ="#4E84C4",
                               SI = "#E7B800")) +
  scale_color_manual(name = "", 
                     values = "#3ADF00", 
                     labels = "Green Growth Index") +
  xlab("Region") + ylab("Dimensions of green growth")+
  theme_bw() +
  theme(legend.position = "right",
        legend.title = element_blank(),
        legend.spacing.x = unit(0.3, 'cm'))

p

enter image description here

Data preparation code.

This code uses the built in data set iris. I take advantage of its structure and change column names and factor values.

df1 <- iris
names(df1) <- c("GEO", "NC", "RE", "SI", "Africa")
df1$Africa <- c(rep("Eastern Africa", 50),
                rep("Middle Africa", 50),
                rep("Western Africa", 50))

new_africa <- reshape2::melt(df1, id.vars = "Africa")
names(new_africa)[2] <- "dimension"
new_africa$groupMeans <- ave(new_africa$value, new_africa$Africa, FUN = mean)
new_africa$mean <- "Mean"
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Hi, thank you very much for the solution. Do you perhaps know the way to join the horizontal lines? Thank you once again! :) – tropicalbath Aug 26 '19 at 18:41
  • @tropicalbath Glad it is usefull. And no, I din't know how to join the lines but it doesn't seem to be easy, the joining lines would go to an area with nothing plotted. – Rui Barradas Aug 26 '19 at 18:51