Titles & labels on ggplot double line graph - r

I am trying to add a title and change the axis names on a double line graph using ggplot. my r code...
aa1 = read.table("C:/pathway/data.txt", header=TRUE)
aa1$Year <- factor(aa1$Year)
aa2 <- ddply(aa1, c("Type", "Year"), summarise, length=mean(Percent))
ggplot(aa2, aes(x=Year, y=length, colour=Type, group=Type)) + geom_line() + geom_point(size=4) + ylim(0, max(aa2$length))
I have made several attempts at this, but can't seem to make any progress. Any help, including alternatives to using ggplot, would be greatly appreciated.

You can add axis labels using xlab() and ylab(). You can add a title using ggtitle()
Try:
ggplot(aa2, aes(x=Year, y=length, colour=Type, group=Type)) + geom_line() + geom_point(size=4) + ylim(0, max(aa2$length)) + xlab("x-axis") + ylab("y-axis") + ggtitle("graph title")

Related

add geom vline and ggplotly to a facet grid

is there any way i could add a vertical line in both the plot at x=15. and also add a plotly to this. i tried but it doesn't seem to work. Thanks
sbucks_new %>%
ggplot(aes(x= category, y= bad_fat, color= category)) +
geom_boxplot() +
coord_flip() +
facet_grid(~ milk_dummy)+
labs(title= "Unhealthy Fats in Milk drinks by Category",
x= "Drinks Category",
y="Bad Fats (g)") +
theme_bw()
Use geom_hline for plotting the vertical line (confusing due to the coord_flip).
Here's an example with mtcars:
p <- ggplot(mtcars, aes(x=factor(carb), y=disp)) +
geom_boxplot() +
facet_wrap(~am2) +
geom_hline(aes(yintercept=300)) +
coord_flip()
Not sure about your other question, but you can quickly convert ggplot object into plotly using ggplotly function.
plotly::ggplotly(p)

Labelling plots arranged with grid.arrange

I have attached multiple plots to one page using grid.arrange.
Is there a way to label each plot with "(a)","(b)" etc...
I have tried using geom_text but it does not seem compatible with my plots....
.... as you can see, geom_text has some strange interaction with my legend symbols.
I will show an example using the mtcars data of what I am trying to achieve. THe alternative to geom_text I have found is "annotate" which does not interact with my legend symbols. However, it is not easy to label only one facet....
q1=ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(a)",size=8,family="serif")
q2=ggplot(mtcars, aes(x=mpg, y=wt,)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(b)",size=8,family="serif")
geom_text(x=15, y=5,size=8, label="(b)")
gt1 <- ggplotGrob(q1)
gt2 <- ggplotGrob(q2)
grid.arrange(gt1,gt2, ncol=1)
Therefore, my question is, is there a way to label plots arranged using grid.arrange, so that the first facet in each plot is labelled with either a, or b or c etc...?
You can use ggarrange from ggpubr package and set labels for each plot using the argument labels:
library(ggplot2)
library(ggpubr)
q1=ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(a)",size=8,family="serif")
q2=ggplot(mtcars, aes(x=mpg, y=wt,)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(b)",size=8,family="serif")
ggarrange(q1,q2, ncol = 1, labels = c("a)","b)"))
Is it what you are looking for ?
If you set inherit.aes=FALSE, you can prevent it from interring:
ggplot(mtcars, aes(x=mpg, y=wt,col=factor(cyl))) +
geom_line() +
geom_point()+
geom_text(inherit.aes=FALSE,aes(x=15,y=12,label="(a)"),
size=8,family="serif")+
facet_grid(~cyl)
If you want to only label the first facet (hope I got you correct), I think the easiest way to specify a data frame, e.g if we want only something in the first,
#place it in the first
lvl_data = data.frame(
x=15,y=12,label="(a)",
cyl=levels(factor(mtcars$cyl))[1]
)
ggplot(mtcars, aes(x=mpg, y=wt,col=factor(cyl))) +
geom_line() +
geom_point()+
geom_text(data=lvl_data,inherit.aes=FALSE,
aes(x=x,y=y,label=label),size=8,family="serif")+
facet_grid(~cyl)

R - geom_point - grouping to be used for legend

I have two geom_point commands applied to different data frames and would like to have a legend to specify them. However, I am not sure how to group them right for the legend. I appreciate it if you can take a look at the simple example below and help me figure out why no legend appears on the figure. Thanks!
df1=data.table(x1=c(-1,0,1), y1=c(-1,0,1))
df2=data.table(x2=c(-1,0,1), y2=c(-2,0,2))
ggplot()+
geom_point(data=df1, aes(x=x1, y=y1), color='red', group=1) +
geom_point(data=df2, aes(x=x2, y=y2), color='blue', group=2) +
xlab("X Label")+ylab("Y Label") +
scale_colour_manual(name = "My Legend",
values = group,
labels = c("database1", "database2"))
As suggested, ggplot2 likes a "tidy" way of dealing with data. In this case, it involves combining the data with an additional variable to differentiate the groups:
colnames(df2) <- c("x1","y1")
df <- rbind(transform(df1, grp='red'), transform(df2, grp='blue'))
ggplot()+
geom_point(data=df, aes(x=x1, y=y1, color=grp), group=1) +
xlab("X Label")+ylab("Y Label") +
scale_color_identity(guide="legend")
I used scale_color_identity for simplicity here, but it isn't hard to use where you started going with scale_colour_manual and relabeling them.

Apply coord_flip() to single layer

I would like to have a boxplot showing the same distribution underneath my histogram.
The code below almost works, but coord_flip() is being applied to all layers, instead of just the geom_boxplot layer.
plot1<-ggplot(newdatahistogram, aes_string(x=newdatahistogram[RawLocation])) +
xlab(GGVar) + ylab("Proportion of Instances") +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", fill="white",origin=-0.5) +
scale_x_continuous(limits=c(-3,6), breaks=seq(0,5,by=1), expand=c(.01,0)) +
geom_boxplot(aes_string(x=-1, y=newdatahistogram[RawLocation])) + coord_flip()
How can I apply coord_flip() to a single layer?
Thank you!
I got it to work with a bit of a hack;
plot1 <- ggplot(newdatahistogram, aes_string(x=newdatahistogram[RawLocation], fill=(newdatahistogram[,"PQ"]))) +
xlab(GGVar) + ylab("Proportion of Observation") +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", origin=-0.5) +
scale_x_continuous(limits=c(-1,6), breaks=seq(0,5,by=1), expand=c(.01,0)) +
scale_y_continuous(limits=c(-.2,1), breaks=seq(0,1,by=.2))
theme(plot.margin = unit(c(0,0,0,0), "cm"))
plot_box <- ggplot(newdatahistogram) +
geom_boxplot(aes_string(x=1, y=newdatahistogram[RawLocation])) +
scale_y_continuous(breaks=(0:5), labels=NULL, limits=c(-1,6), expand=c(.0,-.03)) +
scale_x_continuous(breaks=NULL) + xlab(NULL) + ylab(NULL) +
coord_flip() + theme_bw() +
theme(plot.margin = unit(c(0,0,.0,0), "cm"),
line=element_blank(),text=element_blank(),
axis.line = element_blank(),title=element_blank(), panel.border=theme_blank())
PB = ggplotGrob(plot_box)
plot1 <- plot1 + annotation_custom(grob=PB, xmin=-1.01, xmax=5.95, ymin=-.3,ymax=0)
This saves the rotated boxplot as a grob object and inserts it into the plot under the histogram.
I needed to play with the expansion element a bit to get the scales to line up,
but it works!
Seriously though, I think ggplot should have a horizontal boxplot available without cord_flip()... I tried to edit the boxplot code, but it was way too difficult for me!
Tried to post image, but not enough reputation
You can't: coord_flip always acts on all layers. However, you do have two alternatives:
The solution here shows how to use grid.arrange() to add a marginal histogram. (The comments in the question also link to a nice base-R way to do the same thing)
You could indicate density using a rug plot on of the four sides of the plot with plot1 + geom_rug(sides='r')
ggplot(mpg, aes(x=class, y=cty)) +
geom_boxplot() + geom_rug(sides="r")

Subscripts in xlabels in ggplot2

The following commands
library(ggplot2)
ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
produce this graph.
. I wonder how to get xlabels as ctrl, trt_1 and trt_2, here 1 and 2 are in subscripts. As I need to have the graphs in png format so I'm avoiding tikzDevice and pgfSweave. Thanks in advance for your help.
here is an example:
ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() +
scale_x_discrete(breaks = unique(PlantGrowth$group), labels = c(expression(ctrl), expression(trt[1]), expression(trt[2])))

Resources