I can create a faceted plot like so, with 3 plots stacked vertically:
ggplot(iris, aes(Petal.Length)) + stat_bin() + facet_grid(Species ~ .)
Is it possible to move the labels to the top of each graph, like they would be if I'd done horizontal stacking with facet_grid(. ~ Species)?
The reason I want this is that my plots are long time series plots, so I want the full width for each one, but the labels (which essentially function as titles to explain the facets) for each plot are too long to fit in the small label area at the right of the plot.
Yes. Use facet_wrap instead of facet_grid and be sure to also specify the argument ncol=1:
ggplot(iris, aes(Petal.Length)) + stat_bin() + facet_wrap(~Species, ncol=1)
Try this:
ggplot(iris, aes(Petal.Length)) + stat_bin() + facet_wrap(~Species,nrow = 3)
Related
I want to overlay two sets of ggplot panels (each panel is a different country) into one, single ggplot panel, without any rescaling of any of the two plots, but ggplot rescales either one or the other.
I have tried using only one ggplot to include both variables, by doing ggplot(df, aes(x=t, y=a)), and, within that ggplot, then using geom_point and geom_smooth for the second variable (y=b), but this rescales variable a.
# plot 1
g <-ggplot(df, aes(x=year, y=a))
p <-g + geom_point(alpha=0.7) + geom_smooth(method="auto") + facet_wrap(~country, scales="free") + theme_bw() +
xlab("Year") + ylab(bquote('a')) +
scale_x_continuous(breaks=seq(1960, 2020, 15))
# plot 2
a <-ggplot(df, aes(x=year, y=b))
b <-a + geom_point(alpha=0.7, color="green") + geom_smooth(method="auto", color="darkgreen") +
facet_wrap(~country, scales="free") + theme_bw() +
xlab("Year") + ylab(bquote('b')) +
scale_x_continuous(breaks=seq(1960, 2020, 15))
I expect to be able to overlay these two ggplots into a single set of panels, with both y-axes appearing exactly as they appear when they're plotted alone (including units). I would then need to somehow make one of the y-axis appear to the right of the panels, so I have two y-axes, one at each side.
Image 1. ggplot rescales left y-axis. I don't want this to happen.
Image 2. What I want instead is to be able to somehow merge each of these images to get a single panel per country, displaying both the green and the blue lines with the scales that appear here.
I'll start with a simple faceted histogram using mtcars, faceting on the variables vs and am.
library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(wt)) +
geom_histogram() +
facet_grid(vs ~ am)
I want to have the x-axis label underneath each facet so switch to facet_wrap.
ggplot(mtcars, aes(wt)) +
geom_histogram() +
facet_wrap(vs ~ am, nrow = 2, scales = "free")
Now the strips for vs and am are stacked. How can I make the strips for am go across the top and the strips for vs appear on the right side as is the case in the first histogram using facet_grid?
In R, I'm trying to make a boxplot in ggplot with flipped coordinates (horizontal boxes) grouped using facets. When I build this without flipping coordinates, ggplot will drop unused factor levels within facets with scales="free", but this doesn't seem to work when I also include coord_flip.
Minimal example:
library('ggplot2')
dat <- data.frame(RESP=rnorm(60), GROUP=rep(letters[1:6],each=10), FACET=c(rep(LETTERS[1:2],each=25),rep(LETTERS[3],10)))
The normal faceted boxplot wihtout dropping unused levels works (but not what I want):
ggplot(dat, aes(x=GROUP, y=RESP)) +
geom_boxplot() +
facet_grid(.~FACET)
The normal faceted boxplot with dropped levels also works fine (not what I want):
ggplot(dat, aes(x=GROUP, y=RESP)) +
geom_boxplot() +
facet_grid(.~FACET, scales="free", space="free")
The faceted boxplot with flipped coordinates (what I want) does not drop the unused levels:
ggplot(dat, aes(x=GROUP, y=RESP)) +
geom_boxplot() +
facet_grid(FACET~., scales="free", space="free") +
coord_flip()
Re-arranging the order of the ggplot commands doesn't fix it. I suspect the answer is in some adjustment of the FACET~. formula, but can't solve it.
It is an issue of ggplot2: coord_flip and free scales don't work
You can read a discussion about this matter here:
How to drop unused factors in faceted R ggplot boxplot?
In ggplot2, coord_flip and free scales don't work together
data:
df<-data.frame(grp=letters[1:4],perc=runif(4))
First option:
First, create a second dataset that contains zeros for each group
df2<-rbind(df,data.frame(grp=df[,1],perc=c(0,0,0,0)))
Then plot with geom_points and geom_line:
ggplot(df,aes(y=perc,x=grp))+
geom_point()+
geom_line(data=df2, aes(y=perc, x=grp))+
coord_flip()
Which looks just fine. Just too much extra work to create a second dataset.
The other option is using geom_bar and making the width tiny:
ggplot(df,aes(y=perc,x=grp))+
geom_point()+
geom_bar(stat="identity",width=.01)+
coord_flip()
But this is also weird, and when I save to .pdf, not all of the bars are the same width.
There clearly has to be an easier way to do this, any suggestions?
Use geom_segment with fixed yend = 0. You'll also need expand_limits to adjust the plotting area:
ggplot(df, aes(y=perc, x=grp)) +
geom_point() +
geom_segment(aes(xend=grp), yend=0) +
expand_limits(y=0) +
coord_flip()
I am trying to change the style settings of this kind of chart and hope you can help me.
R code:
set_theme(theme_bw)
cglac$pred2<-as.factor(cglac$pred)
ggplot(cglac, aes(x=depth, colour=pred2))
+ geom_bar(aes(y=..density..),binwidth=3, alpha=.5, position="stack")
+ geom_density(alpha=.2)
+ xlab("Depth (m)")
+ ylab("Counts & Density")
+ coord_flip()
+ scale_x_reverse()
+ theme_bw()
which produces this graph:
Here some points:
What I want is to have the density line as black and white lines separated by symbols rather than colour (dashed line, dotted line etc).
The other thing is the histogram itself. How do I get rid of the grey background in the bars?
Can I change the bars also to black and white symbol lines (shaded etc)? So that they would match the density lines?
Last but not least I want to add a second x or in this case y axis, because of flip_coord(). The one I see right now is for the density. The other one I need would then be the count data from the pred2 variable.
Thanks for helping.
Best,
Moritz
Have different line types: inside aes(), put linetype = pred2. To make the line color black, inside geom_density, add an argument color = "black".
The "background" of the bars is called "fill". Inside geom_bar, you can set fill = NA for no fill. A more common approach is to fill in the bars with the colors, inside aes() specify fill = pred2. You might consider faceting by your variable, + facet_wrap(~ pred2, nrow = 1) might look very nice.
Shaded bars in ggplot? No, you can't do that easily. See the answers to this question for other options and hacks.
Second y-axis, similar to the shaded symbol lines, the ggplot creator thinks a second y-axis is a terrible design choice, so you can't do it at all easily. Here's a related question, including Hadley's point of view:
I believe plots with separate y scales (not y-scales that are transformations of each other) are fundamentally flawed.
It's definitely worth considering his point of view, and asking yourself if those design choices are really what you want.
Different linetypes for densities
Here's my built-in data version of what you're trying to do:
ggplot(mtcars, aes(x = hp,
linetype = cyl,
group = cyl,
color = cyl)) +
geom_histogram(aes(y=..density.., fill = cyl),
alpha=.5, position="stack") +
geom_density(color = "black") +
coord_flip() +
theme_bw()
And what I think you should do instead. This version uses facets instead of stacking/colors/linetypes. You seem to be aiming for black and white, which isn't a problem at all in this version.
ggplot(mtcars, aes(x = hp,
group = cyl)) +
geom_histogram(aes(y=..density..),
alpha=.5) +
geom_density() +
facet_wrap(~ cyl, nrow = 1) +
coord_flip() +
theme_bw()