0

I have this data:

df <- structure(list(Study = 1:5, contributions = c(10L, 15L, 20L, 
50L, 5L)), .Names = c("Study", "contributions"), class = "data.frame", row.names = c(NA, 
-5L))

I want to generate a barplot. I am able to do that using this code:

par(mar = c(5, 10, 5, 5))
barplot(df$contributions, names = df$Study,
        xlim = c(0, 60), las = 2, horiz = T,
        col = "royalblue")

I need help in reproducing the plot generated by the above code but using using ggplot2 library. This would make it easier for me to save and select proper resolution for the plot.

I reviewed this and tried:

library(ggplot2)
plotobj <- NULL 
plotobj <- ggplot(df,aes(x=contributions,y=Study)) 
plotobj <- plotobj + geom_bar(stat="identity",fill="royalblue") 
plotobj 

But it didn't work.

Community
  • 1
  • 1
daragh
  • 173
  • 1
  • 11
  • @Pascal `plotobj <- NULL plotobj <- ggplot(df,aes(x=contributions,y=Study)) plotobj <- plotobj + geom_bar(stat="identity",fill="royalblue") plotobj` – daragh Oct 26 '15 at 02:24
  • Does `ggplot(df, aes(x=Study, y=contributions)) + geom_bar(stat="identity", fill="royalblue") + coord_flip()` do what you want? –  Oct 26 '15 at 02:33
  • Yes it does. Thank you. – daragh Oct 26 '15 at 03:51

1 Answers1

1

You can use coord_flip function:

library(ggplot2)
ggplot(df, aes(x=Study, y=contributions)) + 
  geom_bar(stat="identity", fill="royalblue") + 
  coord_flip()

enter image description here