Reputation: 1054
I have the following data frame:
df<- read.table(text = "months ratio_1 ratio_2
1 January 11.757426 18.047800
2 February 12.515489 20.544807
3 March 12.703583 18.818962
4 April 11.348465 15.229768
5 May 13.366337 12.030971
6 June 15.371622 12.866768
7 July 13.157895 7.711387
8 August 12.939002 11.344097
9 September 8.401084 16.298587
10 October 10.494753 14.334838
11 November 8.695652 19.626384
12 December 11.248285 16.640037", header = TRUE, sep = "")
and I want to create a grouped bar chart. I used plotly and the result is as I want to be:
However, I realized that in order to download it as eps format, I have to pay a subscription. For that reason, I tried ggplot. This is what I tried using the tutorials:
gplot(df, aes(months, ratio_1)) +
+ geom_bar(aes(fill = ratio_2), position = "dodge", stat="identity")
The result is not what I actually want:
Upvotes: 0
Views: 570
Reputation: 4761
You need to reshape the data to a long format and then use ggplot. Don't forget setting the stat to identity and the position to "dodge "("stacked" by default).
library(ggplot2)
library(reshape
ggplot(melt(df,id.vars="months"),aes(x=months,y=value,fill=variable))+
geom_bar(stat="identity",position="dodge")+
theme_bw()+
scale_fill_discrete(labels=c("Ratio 1","Ratio2"),name=NULL)
And if you want it to look somewhat like your original you could do:
ggplot(melt(df,id.vars="months"),aes(x=months,y=value,fill=variable))+
geom_bar(stat="identity",position="dodge")+
theme_bw()+
scale_fill_manual(labels=c("ratio 1","ratio2"),name=NULL,values = c("blue", "orange"))+
ylab("Percentage %")+
xlab("Months")
Upvotes: 2