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])))
Related
I need to plot the geom_histogram but as the plots overlap, I want to plot it linear instead of bars, i.e, the x=hp, y=count (percent).
Can anybody please help me find how to do it.
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x=hp)) +
geom_histogram(binwidth=5)
I have done it by qplot but I need to do it in ggplot and the percent.
qplot(hp, data = mtcars, geom = "freqpoly", binwidth = 10)
Thanks
You can use geom_freqpoly:
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x=hp, y=..count../sum(..count..))) +
geom_freqpoly(binwidth=5) +
scale_y_continuous(labels = percent_format()) +
ylab("Percent")
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)
I want to have a horizontal bar chart, but without legend. When I run the script below without coord_flip(), no legend shows. But when I run it with the coord_flip() argument, the legend appaers. I think this might be a bug. Does anyone know a different code with which I can skip the legend?
df <- aggregate(formula=mpg~cyl, data=mtcars, FUN=sum)
fig <- ggplot(df, aes(x=cyl, y=mpg, fill=cyl, label=mpg)) +
geom_col() +
coord_flip() +
theme(legend.position="none") +
theme_bw()
fig
until now I can't find an appropriate answer, here is my short question about ggplot2 in R:
data(mtcars)
ggplot(data=mtcars, aes(x=mpg, y=wt, fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue"))+
geom_point(size=2, pch=21)+
facet_grid(.~cyl)
This is all fine, now I want all data points (regardless what number cyl has) in every facet (e.g. with a smooth grey below the points)?
Thanks, Michael
Here is a slight simplification that makes life a bit easier. The only thing you need to do is to remove the faceting variable from the data provided to the background geom_point() layer.
library(tidyverse)
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
geom_point(data=select(mtcars,-cyl), colour="grey") +
geom_point(size=2, pch=21, aes(fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue")) +
facet_wrap(~cyl)
Using the link given by #beetroot, I was able to do something like this :
g1 <- ggplot(data=mtcars, aes(x=mpg, y=wt)) +
geom_point(data=mtcars[, c("mpg", "wt")], aes(x=mpg, y=wt), colour="grey") +
geom_point(size=2, pch=21, aes(fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue")) +
facet_wrap(~cyl)
This produces the plot :
Hope this helps you.
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")