Marna Wagley
2016-Aug-31 23:35 UTC
[R] How can I add text in plot and x axis of figures created in ggplot2?
Hi R users, I have created four figures using ggplot2, but I am having trouble to add "r2=XXX, p=XX" value on the upper left in each figure and also unit of X axis of each figure are different. I was also trying to write following : 1. "rainfall (mm/year") on X axix for fig A. 2. "temp (degree Celsius)" on X axis for fig B 3. "distance (m)" on X axis for fig C 4. "survival Proba(%) on X axis for fig D I am wondering how I can create the figures with the above information Thank you for your help in advance Sincerely, Marna following code and the example I have used. dat<-structure(list(x = c(0.31, 0.04, 0.1, 0.54, 0.03, 0.86, 0.97, 0.4, 0.62, 0.3, 0.44, 0.51, 0.03, 0.12, 0.79, 0.3, 0.22, 0.66, 0.75, 0.45), y = c(0.38, 0.61, 0.16, 0.06, 0.42, 0.67, 0.85, 0.11, 0.79, 0.21, 0.84, 0.95, 0.3, 0.47, 0.79, 0.2, 0.34, 0.21, 0.62, 0.25), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("A", "B", "C", "D"), class = "factor")), .Names = c("x", "y", "group" ), class = "data.frame", row.names = c(NA, -20L)) gp<-ggplot(data=dat, aes(x=x, y=y)) Gp<-gp + geom_point(size=1, col="blue") Gp<-Gp+ stat_smooth(method="lm", level=0.99, col="black",formula=y~poly(x,1 ))+ coord_cartesian(ylim=c(0, 1))+theme_bw()+ theme(axis.text.y = element_text(angle = 90, vjust = 0))+ ylab <http://docs.ggplot2.org/0.9.2.1/labs.html>*(*"My Y"*)+*theme( axis.text.x = element_text(size=8)) Gp+ facet_wrap(~group,ncol=5, scales="free_x") [[alternative HTML version deleted]]
John Kane
2016-Sep-01 13:45 UTC
[R] How can I add text in plot and x axis of figures created in ggplot2?
Hi Marna, Thanks for providing the data and code. However there is a problem with the code and I wondered if something got garbled in transmission. Below is the code that was in my mailbox =================================================ylab <http://docs.ggplot2.org/0.9.2.1/labs.html>*(*"My Y"*)+*theme( axis.text.x = element_text(size=8)) ================================================Clearly not functioning code Am I correct in assuming that what it originally looked like was : =============================================ylab("http://docs.ggplot2.org/0.9.2.1/labs.html") + theme(axis.text.x = element_text(size=8)) =============================================? It looks like you are sending mail in HTML and that can badly mangle code and data layouts. Please send an messages to R-help in plain text. I don't use facet_wrap() enough to know if what you want to do is doable but I am a bit dubious. I wonder if you have considered creating 4 independent graphs and using grid.Extra or grid.arrange to get the results you want.? Sorry not to be of more help John Kane Kingston ON Canada> -----Original Message----- > From: marna.wagley at gmail.com > Sent: Wed, 31 Aug 2016 16:35:29 -0700 > To: r-help at r-project.org > Subject: [R] How can I add text in plot and x axis of figures created in > ggplot2? > > Hi R users, > I have created four figures using ggplot2, but I am having trouble to > add > "r2=XXX, p=XX" value on the upper left in each figure and also unit of X > axis of each figure are different. I was also trying to write following > : > 1. "rainfall (mm/year") on X axix for fig A. > 2. "temp (degree Celsius)" on X axis for fig B > 3. "distance (m)" on X axis for fig C > 4. "survival Proba(%) on X axis for fig D > > I am wondering how I can create the figures with the above information > > Thank you for your help in advance > > Sincerely, > > Marna > > following code and the example I have used. > > dat<-structure(list(x = c(0.31, 0.04, 0.1, 0.54, 0.03, 0.86, 0.97, > > 0.4, 0.62, 0.3, 0.44, 0.51, 0.03, 0.12, 0.79, 0.3, 0.22, 0.66, > > 0.75, 0.45), y = c(0.38, 0.61, 0.16, 0.06, 0.42, 0.67, 0.85, > > 0.11, 0.79, 0.21, 0.84, 0.95, 0.3, 0.47, 0.79, 0.2, 0.34, 0.21, > > 0.62, 0.25), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, > > 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("A", > > "B", "C", "D"), class = "factor")), .Names = c("x", "y", "group" > > ), class = "data.frame", row.names = c(NA, -20L)) > > > gp<-ggplot(data=dat, aes(x=x, y=y)) > > Gp<-gp + geom_point(size=1, col="blue") > > Gp<-Gp+ stat_smooth(method="lm", level=0.99, > col="black",formula=y~poly(x,1 > ))+ > > coord_cartesian(ylim=c(0, 1))+theme_bw()+ > > theme(axis.text.y = element_text(angle = 90, vjust = 0))+ > > ylab <http://docs.ggplot2.org/0.9.2.1/labs.html>*(*"My Y"*)+*theme( > axis.text.x = element_text(size=8)) > > Gp+ facet_wrap(~group,ncol=5, scales="free_x") > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.____________________________________________________________ Can't remember your password? Do you need a strong and secure password? Use Password manager! It stores your passwords & protects your account.
Ulrik Stervbo
2016-Sep-01 14:00 UTC
[R] How can I add text in plot and x axis of figures created in ggplot2?
Hi Marna, when you use facet there is just one X-axis title and one Y-axis title. As an alternative you can create several plots and put them into one using the package gridExtra. As for adding text you can use geom_text and extract the x, y coordinates from your data. When I add text, I usually create a separate data.frame which in addition to the text I want to add and the positions also contains indications of the global aesthetic and the facet column Hope this helps Ulrik On Thu, 1 Sep 2016 at 15:48 John Kane <jrkrideau at inbox.com> wrote:> Hi Marna, > > Thanks for providing the data and code. However there is a problem with > the code and I wondered if something got garbled in transmission. > > Below is the code that was in my mailbox > =================================================> ylab <http://docs.ggplot2.org/0.9.2.1/labs.html>*(*"My Y"*)+*theme( > axis.text.x = element_text(size=8)) > ================================================> Clearly not functioning code > > Am I correct in assuming that what it originally looked like was : > =============================================> ylab("http://docs.ggplot2.org/0.9.2.1/labs.html") + > theme(axis.text.x = element_text(size=8)) > =============================================> ? > > It looks like you are sending mail in HTML and that can badly mangle code > and data layouts. Please send an messages to R-help in plain text. > > I don't use facet_wrap() enough to know if what you want to do is doable > but I am a bit dubious. > > I wonder if you have considered creating 4 independent graphs and using > grid.Extra or grid.arrange to get the results you want.? > > Sorry not to be of more help > > John Kane > Kingston ON Canada > > > > -----Original Message----- > > From: marna.wagley at gmail.com > > Sent: Wed, 31 Aug 2016 16:35:29 -0700 > > To: r-help at r-project.org > > Subject: [R] How can I add text in plot and x axis of figures created in > > ggplot2? > > > > Hi R users, > > I have created four figures using ggplot2, but I am having trouble to > > add > > "r2=XXX, p=XX" value on the upper left in each figure and also unit of X > > axis of each figure are different. I was also trying to write following > > : > > 1. "rainfall (mm/year") on X axix for fig A. > > 2. "temp (degree Celsius)" on X axis for fig B > > 3. "distance (m)" on X axis for fig C > > 4. "survival Proba(%) on X axis for fig D > > > > I am wondering how I can create the figures with the above information > > > > Thank you for your help in advance > > > > Sincerely, > > > > Marna > > > > following code and the example I have used. > > > > dat<-structure(list(x = c(0.31, 0.04, 0.1, 0.54, 0.03, 0.86, 0.97, > > > > 0.4, 0.62, 0.3, 0.44, 0.51, 0.03, 0.12, 0.79, 0.3, 0.22, 0.66, > > > > 0.75, 0.45), y = c(0.38, 0.61, 0.16, 0.06, 0.42, 0.67, 0.85, > > > > 0.11, 0.79, 0.21, 0.84, 0.95, 0.3, 0.47, 0.79, 0.2, 0.34, 0.21, > > > > 0.62, 0.25), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, > > > > 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("A", > > > > "B", "C", "D"), class = "factor")), .Names = c("x", "y", "group" > > > > ), class = "data.frame", row.names = c(NA, -20L)) > > > > > > gp<-ggplot(data=dat, aes(x=x, y=y)) > > > > Gp<-gp + geom_point(size=1, col="blue") > > > > Gp<-Gp+ stat_smooth(method="lm", level=0.99, > > col="black",formula=y~poly(x,1 > > ))+ > > > > coord_cartesian(ylim=c(0, 1))+theme_bw()+ > > > > theme(axis.text.y = element_text(angle = 90, vjust = 0))+ > > > > ylab <http://docs.ggplot2.org/0.9.2.1/labs.html>*(*"My Y"*)+*theme( > > axis.text.x = element_text(size=8)) > > > > Gp+ facet_wrap(~group,ncol=5, scales="free_x") > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ____________________________________________________________ > Can't remember your password? Do you need a strong and secure password? > Use Password manager! It stores your passwords & protects your account. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]