users,
I have a continuous x-variable from 1-999 (I provide a sample of 20 observations of my data below to use).
I would like to plot some line graphs and a bar chart on the same plot such as this plot. The variable that I would like to plot on the bar chart is the variable "Irrigationtotal".
I have two problems: 1) the first problem is that for the bar chart, R only accepts discrete values on the x-asis. Therefore, it changes the order of my continuous values like: 1, 11, 101..., 2, 21, 22, ... 200, ... 3 33... And obviously, I want to have 1, 2, 3, 4, 5...
I realize I can use scale_x_discrete (limits = ...) for this problem, but then I think I will not be able to plot the line graph on the same plot as the bar chart.
2) The second problem is that I don't manage to get them nicely on top of each other while sharing the same x-axis.
Below you can find the code I have for the two graphs separately. Could somebody help with the problems described above? Thank you very much!
The data
out122<- structure(list(MEt_R = c(-0.0541818151603231, -0.0562844791428272,
-0.0558715941992024, -0.0562399962945622, -0.0560460386125185,
-0.0570608897132082, -0.0569943385875705, -0.0568252787782472,
-0.0569942506473323, -0.0565197621205338, -0.056900534973487,
-0.0571427349989937, -0.0569618449465491, -0.0566601716889117,
-0.0563552308197707, -0.0568648464047371, -0.057047451157018,
-0.0571837090302319, -0.0588902340655496, -0.0592472918164029
), MEt_Irr = c(-0.0930626749780042, -0.0924059309460578, -0.0924771937440385,
-0.0905386156125412, -0.0914934037180768, -0.0898948119109486,
-0.0898827200499507, -0.090372707751177, -0.0901901622784647,
-0.0914484064620663, -0.0925147845884521, -0.0927733849042059,
-0.0960873954367445, -0.0948131376144847, -0.0955133693827158,
-0.0933133384990093, -0.0927340360155418, -0.0925138612415783,
-0.0896139882242573, -0.0912014136494108), se_MEt_Rainfed = c(0.124867384884912,
0.124157398945455, 0.124169568385358, 0.124110270348855, 0.12391954965997,
0.123742628011372, 0.123766054757713, 0.123576175335345, 0.12353428904291,
0.123443556846824, 0.122869340273675, 0.122594726299249, 0.122332685310317,
0.12197210341919, 0.121115745201095, 0.120880251090657, 0.120851770150267,
0.120746714650168, 0.120922991632831, 0.120866928018865), se_MEt_Irrigation = c(0.0790595725119853,
0.0819113174332981, 0.0818328749299557, 0.0834638025854297, 0.0818357384597404,
0.0830466544695816, 0.0830677796154873, 0.0829941906461297, 0.083141965909444,
0.082714324704666, 0.0809987066350066, 0.0810565659915952, 0.0792023249112186,
0.0779277210970589, 0.0797106575341609, 0.0796897823245035, 0.0793238667046254,
0.0794345101645159, 0.0805370559814554, 0.0816802765047257),
Irrigationtotal = c(3610, 3605, 3599, 3597, 3593, 3590, 3589,
3584, 3578, 3573, 3562, 3555, 3551, 3544, 3538, 3536, 3530,
3528, 3519, 3512)), .Names = c("MEt_R", "MEt_Irr", "se_MEt_Rainfed",
"se_MEt_Irrigation", "Irrigationtotal"), row.names = c(NA, 20L
), class = "data.frame")
Line graph
out122$perc<-c(1:20)
l64<-ggplot(out122,aes(perc))
l65<-l64+geom_line(aes(y=MEt_R,colour="Rainfed"))+
geom_line(aes(y=MEt_R+se_MEt_Rainfed,colour="Rainfed range"))+
geom_line(aes(y=MEt_R-se_MEt_Rainfed,colour="Rainfed range"))+
geom_line(aes(y=MEt_Irr,colour="Irrigation"))+
geom_line(aes(y=MEt_Irr+se_MEt_Irrigation,colour="Irrigation range"))+
geom_line(aes(y=MEt_Irr-se_MEt_Irrigation,colour="Irrigation range"))+
scale_colour_manual(values=c("blue","light blue","green","light green"), name="")+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="MEt",limits = c(-0.20, 0.5))+
guides(fill=guide_legend(title=NULL))
l66<-l65+ theme_bw()+ggtitle("subsidies 1 large") +
theme(plot.title = element_text(lineheight=.8, face="bold"))
l66
Bar Graph (here I have the problem with the x-axis which is not ranked like 1, 2, 3...)
l1<- ggplot(data=out122, aes(x=rownames(out122), y=Irrigationtotal, fill=Irrigationtotal)) +
geom_bar(stat="identity")+ theme_bw()
l1