Trying to keep filled bars in a faceted plot - r

Not sure what I'm doing wrong here. I have this plot:
ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge")
which produces:
Then I want to facet by a factor, creating two stacked plots w/ dodged, colored bars
ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge") +
facet_grid(~group_label)
However, I lose the factor-based coloring, which I want to keep:

If you move the fill into the geom_bar it should work. As:
ggplot(data.PE5, aes(ybands)) + geom_bar(aes(fill=factor(decide)),position="dodge") + facet_grid(~group_label)
The reason is the way ggplot2 builds plots as a grammar (I think).

Related

How to color only one meaning of variable histogram ggplot2

I want to make several histograms which are colored by only one meaning of one variable. Something like this
ggplot(data, aes(x=value, fill=attribute)) + geom_histogram()

plot two graphs with different y-axis in ggplot

I'm trying to plot a scatterplot with errorbars together with a barplot. Both variables have a very different range (scatterplot 0 - 20000+; bar chart 0-1). So I want to create a plot with both graphs in it, with two different y-axes. This is what I've got so far:
ggplot(data, aes(x=seaname, y=chao2)) + geom_bar(stat="identity",
aes(x=seaname, y=compl, colour=major_sea)) + geom_point(aes(colour=major_sea))
+ geom_errorbar(aes(ymin=chao2_lower, ymax=chao2_upper,
colour=major_sea)) + theme(axis.text.x=element_text(angle=90,
vjust=0.5, size=9))
Just to clarify: I don't want two graphs represented below each other. I would like them in the same, single graph.
Anyone who has experience with this kind of problem?
Cheers!!

ggplot2: Is there a way to overlay a single plot to all facets in a ggplot

I would like to use ggplot and faceting to construct a series of density plots grouped by a factor. Additionally, I would like to a layer another density plot on each of the facets that is not subject to the constraints imposed by the facet.
For example, the faceted plot would look like this:
require(ggplot2)
ggplot(diamonds, aes(price)) + facet_grid(.~clarity) + geom_density()
and then I would like to have the following single density plot layered on top of each of the facets:
ggplot(diamonds, aes(price)) + geom_density()
Furthermore, is ggplot with faceting the best way to do this, or is there a preferred method?
One way to achieve this would be to make new data frame diamonds2 that contains just column price and then two geom_density() calls - one which will use original diamonds and second that uses diamonds2. As in diamonds2 there will be no column clarity all values will be used in all facets.
diamonds2<-diamonds["price"]
ggplot(diamonds, aes(price)) + geom_density()+facet_grid(.~clarity) +
geom_density(data=diamonds2,aes(price),colour="blue")
UPDATE - as suggested by #BrianDiggs the same result can be achieved without making new data frame but transforming it inside the geom_density().
ggplot(diamonds, aes(price)) + geom_density()+facet_grid(.~clarity) +
geom_density(data=transform(diamonds, clarity=NULL),aes(price),colour="blue")
Another approach would be to plot data without faceting. Add two calls to geom_density() - in one add aes(color=clarity) to have density lines in different colors for each level of clarity and leave empty second geom_density() - that will add overall black density line.
ggplot(diamonds,aes(price))+geom_density(aes(color=clarity))+geom_density()

In ggplot2, how to choose which geom appears in legend?

Some geom obscure the key to other geoms in the legend (notably, boxplot)
How can I choose which geom appears in the legend?
Eg.:
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment)+
geom_boxplot()
Switching the order of the geoms helps
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment,
geom="boxplot")+
geom_point()
But I would like the legend found with:
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment)
Do I need to extract the legend of one plot and paste it to the other using something like gridExtra?
You can suppress the legend for the boxplot by adding show_guide=FALSE to the geom_boxplot() call. You still get the legend from the points.
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment)+
geom_boxplot(show_guide=FALSE)
If you were not already plotting points (that is, just had boxplot, but wanted the legend to be shown with points symbol rather than the boxplot symbol), that is harder, though I think possible.

ggplot2 position='dodge' producing bars that are too wide

I'm interested in producing a histogram with position='dodge' and fill=some factor (i.e. side-by-side bars for different subgroups within each bar/group), but ggplot2 gives me something like the first plot here, which has a rightmost bar that's too wide and reserves no space for the empty group, which I would like.
Here's a simple case:
df = data.frame(a=c('o','x','o','o'), b=c('a','b','a','b'))
qplot(a, data=df, fill=b, position='dodge')
From ggplot geom_bar - bars too wide I got this idea, and while it technically produces a bar of the same width, but preserves no space for the empty group:
ggplot(df, aes(x=a, fill=a))+
geom_bar(aes(y=..count../sum(..count..))) +
facet_grid(~b,scales="free",space="free")
How do I achieve what I want? Thanks in advance.
The default options in ggplot produces what I think you describe. The scales="free" and space="free" options does the opposite of what you want, so simply remove these from the code. Also, the default stat for geom_bar is to aggregate by counting, so you don't have to specify your stat explicitly.
ggplot(df, aes(x=a, fill=a)) + geom_bar() + facet_grid(~b)

Resources