I am trying to build a stack of ggplot2 density plots, like so:
And I have lined up / limited the x-axes so that then these grpahs are stacked using the gridExtra package, the ticks line up perfectly. However, in doing so, what I thought was a solid x-axis marker before turns out be a bottom "marker" line at the bottom of the density plot:
Is there anyway to add back in some sort of x-axis? The plots look somewhat naked/empty without it. I understand it more clearly indicates the limits of the data, but it looks unfinished and broken.
Edit
Here is the code I am using:
g <- ggplot(df_L, aes(x=values, linetype= type)) +
geom_density() +
ggtitle(expression('Low Region: '~LI[i]~'and'~WI[i])) +
scale_x_continuous(breaks = c(seq(0,100,10)), expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(xlim = c(0,100)) +
theme(text = element_text(size=20),
plot.title = element_text(size=14, vjust=1.5, hjust=0.5),
axis.title.x=element_blank(),
axis.title.y = element_blank(),
legend.position = c(0.1, 0.75),
legend.text.align = 0,
legend.box = 'horizontal',
legend.margin = unit(45.0, 'line'),
legend.text=element_text(size=14,vjust=0,hjust=0),
legend.key.height = unit(1, 'line'),
legend.key.width = unit(1, 'line'),
panel.background = element_rect(fill = "white")) +
scale_linetype_manual(values=c(1,2,3),
labels=c(expression(LI[i]),expression(WI[i]))) +
guides(linetype = guide_legend(title=NULL))
g
I think the issue is that in the theme you're using (default) doesn't have a specified x-axis (or y-axis for that matter), but the axis-positions are implied by the grid. So you need to specifically add axis, either by using for instance +theme_bw(), or by adding something in the theme. I have done that (in red, you can really see it):
set.seed(124)
df_L <- data.frame(values=rnorm(1000,500,200),type=sample(LETTERS[1:3],1000,T))
g <- ggplot(df_L, aes(x=values, linetype= type)) +
geom_density() +
ggtitle(expression('Low Region: '~LI[i]~'and'~WI[i])) +
scale_x_continuous(breaks = c(seq(0,100,10)), expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(xlim = c(0,100)) +
theme(text = element_text(size=20),
plot.title = element_text(size=14, vjust=1.5, hjust=0.5),
axis.title.x=element_blank(),
axis.title.y = element_blank(),
legend.position = c(0.1, 0.75),
legend.text.align = 0,
legend.box = 'horizontal',
legend.margin = unit(45.0, 'line'),
legend.text=element_text(size=14,vjust=0,hjust=0),
legend.key.height = unit(1, 'line'),
legend.key.width = unit(1, 'line'),
panel.background = element_rect(fill = "white"),
axis.line=element_line(colour="red",size=2)) +
scale_linetype_manual(values=c(1,2,3),
labels=c(expression(LI[i]),expression(WI[i]))) +
guides(linetype = guide_legend(title=NULL))
Related
I want to add a latex expression in my legend like red-colored. I tried the following code, but I could not do that. How do I manage this issue? Also, I got a waring about this code. How do I remove that?
library(ggplot2)
ggplot(C, aes(sigma, value)) +
geom_line(aes(linetype=name)) +
scale_linetype_manual(values=c("solid", "twodash", "dotted")) +
scale_linetype_discrete(labels = c(expression("X_A = 1 \\& X_B =0"),
expression("X_A = 3 \\& X_B =0"),
expression("X_A = 5 \\& X_B =0"))) +
theme_minimal() +
xlab("standard diviation") +
ylab("rejection rate") +
theme_classic() +
theme(legend.position = c(0.15, 0.85),
legend.background = element_rect(fill="white",
size=0.5,
linetype="solid",
colour ="gray"),
legend.spacing.y = unit(0, "pt"),
text = element_text(size=20),
legend.text = element_text(size=20),
axis.title=element_text(size=20),
legend.title = element_blank(),
panel.grid.major.y = element_line(),
panel.grid.minor.y = element_line())
Scale for 'linetype' is already present. Adding another scale for 'linetype',
which will replace the existing scale.
The following code plots two partly overlapping density distributions from two independent dataframes with different lenghts.
library(ggplot2)
#Define colors to be used in plot for each group
mycolRO <- rgb(0.8, 0.2, 0, max = 1, alpha = 0.5) #Color for Group "Road"
mycolRA <- rgb(0.2, 0.6, 0.4, max = 1, alpha = 0.5) #Color for Group "Rail"
#Create some data
dfRoad <- data.frame(DiffRO=2+rnorm(300))
dfRail <- data.frame(DiffRA=rnorm(500))
#Plot density distributions
ggplot() +
geom_density(aes(x=DiffRO, fill = mycolRO, alpha=0.5), data=dfRoad) +
geom_density(aes(x=DiffRA, fill = mycolRA, alpha=0.5), data=dfRail) +
xlim(-6, 6) +
theme_classic() +
ggtitle("") +
xlab("Value") +
ylab("Density") +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text=element_text(size=15))+
labs(fill = "Group")+
theme(legend.title = element_text(color = "black", size = 15), legend.text = element_text(color = "black", size=12))+
theme(legend.position = c(0.2,0.8), legend.direction = "vertical")+
guides(alpha=FALSE)
The legend does show the correct base color, but not with the transparency (alpha) value defined above, which should be alpha=0.5.
Furthermore I would like to see the correct variable names ("DiffRO" and "DiffRA") as legend entries instead of the color codes.
Thanks for any help.
Here are two ways of doing what you want.
Common points to both are:
The colors are set manually with scale_fill_manual.
theme calls are simplified, there is no need to call theme repeatedly.
First, I will recreate the data, this time setting the RNG seed before calling rnorm.
set.seed(1234)
dfRoad <- data.frame(DiffRO = 2 + rnorm(300))
dfRail <- data.frame(DiffRA = rnorm(500))
Your way, corrected.
The legend labels must also be set manually in scale_fill_manual.
#Plot density distributions
ggplot() +
geom_density(aes(x=DiffRO, fill = mycolRO, alpha=0.5), data=dfRoad) +
geom_density(aes(x=DiffRA, fill = mycolRA, alpha=0.5), data=dfRail) +
xlim(-6, 6) +
ggtitle("") +
xlab("Value") +
ylab("Density") +
scale_fill_manual(labels = c("Road", "Rail"),
values = c(mycolRO, mycolRA)) +
theme_classic() +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text=element_text(size=15),
legend.title = element_text(color = "black", size = 15),
legend.text = element_text(color = "black", size=12),
legend.position = c(0.2,0.8), legend.direction = "vertical")+
labs(fill = "Group") +
guides(alpha = FALSE)
Another way, simpler.
The data is combined and reformated from two different data sets in one data set only. To do this I use package reshape2.
dflong <- reshape2::melt(dfRoad)
dflong <- rbind(dflong, reshape2::melt(dfRail))
Note that now only one call to geom_density is needed and that the legend labels are automatic.
ggplot(dflong, aes(x = value, group = variable, fill = variable, alpha = 0.5)) +
geom_density() +
xlim(-6, 6) +
ggtitle("") +
xlab("Value") +
ylab("Density") +
scale_fill_manual(values = c(mycolRA, mycolRO)) +
theme_classic() +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text = element_text(size=15),
legend.title = element_text(color = "black", size = 15),
legend.text = element_text(color = "black", size=12),
legend.position = c(0.2,0.8), legend.direction = "vertical") +
labs(fill = "Group") +
guides(alpha = FALSE)
I am trying to override my symbol size aes using commands I have found in this forum and elsewhere. I can get the alpha override to work but not the size.
f1<-ggplot(data=d, aes(x=rpos, y=count, group=id,color=id)) +
geom_point(alpha=0.05, size=0.5) +
scale_fill_manual(values=c("blue", "red")) +
scale_colour_manual(values=c("blue", "red")) +
xlab("Chromosome 1") +
scale_y_continuous(name="Relative coverage",limits=c(-0.5,1.5)) +
guides(colour =guide_legend(override.aes=list(size=5))) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
theme_bw()
optns <- theme (
plot.title = element_text(face="bold", size=14),
axis.title.x = element_text(size=12),
axis.title.y = element_text(size=12, angle=90),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(.97,.85),
legend.text = element_text(size=10),
legend.key.size = unit(1, "lines"),
legend.key = element_blank(),
legend.justification = 'right'
)
f1 + ggtitle ("Coverage of Chromosome 1") +
optns + ggsave("Rel.mergedDataChr1.pdf", width = 20, height = 5, dpi = 100)
Your alpha override is overwriting your size override. Change this:
guides(colour = guide_legend(override.aes = list(size=5))) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
to this:
guides(colour = guide_legend(override.aes = list(size=5, alpha = 1))) +
In the future, please try to include minimal working examples. This means both providing data (to make it a working example) and also not bothering with things like all your optns (to keep it minimal).
I have a plot with three ECDFs and three vertical lines, and cannot get the stat_ecdf legend line color to match the actual line color.
Here is the code for just the ECDF plot:
set.seed(124)
allDTI <- data.frame(values=rnorm(1000,500,200),type=sample(LETTERS[1:3],1000,T))
meanALLDTI <- ddply(allDTI, "type", summarise, values.mean=mean(values, na.rm=TRUE), n)
ggplot(allDTI, aes(x=values, color=type)) +
stat_ecdf(size=1, show_guide=T) +
xlab(expression('Index Value')) +
ylab("Cumulative Density") +
ggtitle(expression(TI[d]~'and'~TI[l]~'and'~TI[w])) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
theme(text = element_text(size=20),
plot.title = element_text(size=30,face="bold",vjust=1),
axis.title.x=element_text(size=20,face="bold",vjust=0,hjust=0.5),
axis.title.y=element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
legend.position = c(0.85, 0.25),
legend.text.align = 0,
legend.box = 'horizontal',
legend.margin = unit(45.0, 'line'),
legend.text=element_text(size=28,vjust=0,hjust=0),
legend.key.height = unit(1.5, 'line'),
legend.key.width = unit(1.5, 'line'),
panel.background = element_rect(fill = "white")) +
scale_color_manual(values=c('grey10','grey30','grey50'),
labels=c(expression(TI[d]),expression(TI[l]),
expression(TI[w]))) +
guides(color = guide_legend(title=NULL))
Here is the plot:
But I can't figure out how to add the vertical lines I want to add in the manner I want to add them:
Code:
ggplot(allDTI, aes(x=values, color=type)) +
stat_ecdf(size=1, show_guide=T) +
xlab(expression('Index Value')) +
ylab("Cumulative Density") +
ggtitle(expression(TI[d]~'and'~TI[l]~'and'~TI[w])) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
theme(text = element_text(size=20),
plot.title = element_text(size=30,face="bold",vjust=1),
axis.title.x=element_text(size=20,face="bold",vjust=0,hjust=0.5),
axis.title.y=element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
legend.position = c(0.85, 0.25),
legend.text.align = 0,
legend.box = 'horizontal',
legend.margin = unit(45.0, 'line'),
legend.text=element_text(size=28,vjust=0,hjust=0),
legend.key.height = unit(1.5, 'line'),
legend.key.width = unit(1.5, 'line'),
panel.background = element_rect(fill = "white")) +
scale_color_manual(values=c('grey10','grey30','grey50'),
labels=c(expression(TI[d]),expression(TI[l]),
expression(TI[w]))) +
guides(color = guide_legend(title=NULL)) +
geom_vline(data=meanALLDTI, aes(xintercept=values.mean, colour=type),
linetype="dashed", size=1, show_guide=T) +
scale_linetype_manual(values=c('grey10','grey30','grey50'),
labels=c(expression(bar(TI[d])),expression(bar(TI[l])),
expression(bar(TI[w])))) +
guides(linetype= guide_legend(title=NULL))
Plot:
As an example of what I want, here is how it came out for me with density plots:
After some trial and error, I think it's better to use geom_segment in order to get a nice legend. With:
ggplot(allDTI, aes(x=values, color=type)) +
stat_ecdf(size=1) +
geom_segment(data=meanALLDTI, aes(x=values.mean, xend=values.mean, y=0, yend=1, color=type, linetype = type), size=1) +
labs(title=expression(TI[d]~'and'~TI[l]~'and'~TI[w]), x="Index Value", y="Cumulative Density") +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
scale_color_manual(values=c("grey10", "grey30", "grey50"),
labels=c(expression(TI[d]),expression(TI[l]),expression(TI[w]))) +
scale_linetype_manual(values=c(3,2,5),
labels=c(expression(TI[d]),expression(TI[l]),expression(TI[w]))) +
guides(color = guide_legend(title="color",
override.aes = list(color = c("grey10", "grey30", "grey50"),
size = 2)),
linetype = guide_legend(title="lty", override.aes = list(linetype = c(3,2,5),
size = 0.7))) +
theme(text = element_text(size=20),
plot.title = element_text(size=30,face="bold",vjust=1),
axis.title.x = element_text(size=20,face="bold",vjust=0,hjust=0.5),
axis.title.y = element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
legend.position = c(0.85, 0.25),
legend.box = 'horizontal',
legend.title = element_blank(),
legend.text.align = 0,
legend.text = element_text(size=20,vjust=0,hjust=0),
legend.key = element_rect(fill=NA),
legend.key.height = unit(1.5, 'line'),
legend.key.width = unit(1.5, 'line'),
panel.background = element_rect(fill = "white"))
you get:
This might primarily be a result of me misunderstanding how panel.margin = unit(...) works in the theme() function...but I'm unable to customize margins in facet_wrap the way that I'd like. Basically, I want a facet_grid that looks like this, with facet text (i.e. strip.text) inset in each facet and no spcaing between each facet:
(I've left in the pink borders to show the dimensions of each facet)
So here's the code so far.
To set up the data and plot:
library(ggplot2)
library(grid)
p <- ggplot() +
geom_bar(data = mtcars, aes(x = cyl, y = qsec), stat = 'identity') +
facet_wrap( ~ carb, ncol = 3)
mytheme <- theme_minimal() + theme(
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.margin = unit(0, "lines"),
panel.border = element_rect(colour = rgb(1.0, 0, 0, 0.5), fill=NA, size=1)
)
The standard plot
p + mytheme
Removing the strip.text completely
p + mytheme + theme(strip.text = element_blank())
Adding the strip.text and insetting it
p + mytheme +
theme(strip.text = element_text(size = rel(3.0), vjust = -4.0))
The re-inclusion of strip.text (and the increased relative size) increases the vertical margin between the two rows. So at this point, I don't know how to close the vertical gap between the top and bottom rows.
Too much negative margin
p + mytheme +
theme(strip.text = element_text(size = rel(3.0), vjust = -4.0),
panel.margin = unit(c(-2, -2), "lines"))
So how do I target just the panel.margin between the two rows?
Edit: Additional information. The space between the rows appears to be strip.background:
p + mytheme +
theme(strip.text = element_text(size = rel(3.0), vjust = -4.0),
panel.margin = unit(-1, "lines"),
strip.background = element_rect(fill = rgb(0, 1.0, 0, 0.2)))
Among the list of possible arguments to theme(), there is not only panel.margin ("margin around facet panels (unit)", see ?theme), but conveniently, you can also access one of the axes at a time, with panel.margin.x and panel.margin.y respectively ("horizontal/vertical margin around facet panels (unit; inherits from panel.margin)").
Therefore, while decreasing the margin below zero feels a bit like a hack, something like the following will do the job (you might have to adjust the value a little - unit(-2, "lines") worked best for me):
p + theme(strip.text = element_text(size = rel(3.0), vjust = -4.0),
panel.margin.y = unit(-2, "lines"))
If you use strip.text = element_blank(), then you should probably use panel.margin.y = unit(-0.5, "lines").