I am trying to plot two graphics in one using ggplot2. I have two data frames that share a common variable (factor). They look like this:
tb1:
study caribbean south.america
alison_2010 1 0
james_1998 0 1
...
tb2:
study stage.I stage.II stage III ...
alison_2010 95.6 93.1 81.3
james_1998 94.2 80.7 74.5
...
I would like to plot one graph with both information (results as shown in tb2
and the region of origin as shown in tb1
).
Tb1
would be ploted as a bar plot (to create retangles on the backgound), and tb2
as a dot plot.
I tried this:
tb1<- melt(tb1, id.vars="study")
tb2<- melt(tb2, id.vars="study")
c<-ggplot()+
geom_bar(data=tb1, aes(y=tb1$value, x=tb1$study), fill=tb1$variable),
stat="identity", position_fill(reverse = TRUE))+
geom_dotplot(data=tb2, aes(x=tb2$study, y=tb2$value, color=tb2$variable, fill=tb2$variable),
binaxis="y", stackdir="center", binwidth=1, dotsize=1.5, group=1)
I get this:
When I add + scale_y_continuous(labels=scales::percent)
I get this:
I tried to use 100 insted of 1 on tb1
, or to divide the values in tb2
by 100. Didn't work ;/
I am not worried about the labels or anything at the moment. I just want the bar chart to plot percentage, not counts. Can anyone help me? Thank you!