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

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)

Related

geom_text positions per group

I am using geom_line, geom_point, and geom_text to plot something like the picture below:
I am grouping, and coloring my data frame, but I want the geom_text not to be so close to each other.
I want to put the one text on top, and the other on bottom. Or at least, hide the one of the two. Is there any way I can do this?
You can specify custom aesthetics in different geom_text() calls. You can include only a subset of the data (such as just one group) in each call, and give each geom_text() a custom hjust or vjust value for each subset.
ggplot(dat, aes(x, y, group=mygroups, color=mygroups, label=mylabel)) +
geom_point() +
geom_line() +
geom_text(data=dat[dat$mygroups=='group1',], aes(vjust=1)) +
geom_text(data=dat[dat$mygroups=='group2',], aes(vjust=-1))

grouping bars in bar-plot without using facets

I want to group the bars in a stacked barplot according to the values in another factor-variable. However, I want to do this without using facets.
my data in long format
I want to group the stacked bars according the afk variable. The normal stacked bar plot can be made with:
ggplot(nl.melt, aes(x=naam, y=perc, fill=stemmen)) +
geom_bar(stat="identity", width=.7) +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
coord_flip() +
theme_bw()
which gives an alfabetically ordered barplot:
I tried to group them by using x=reorder(naam,afk) in the aes. But that didn't work. Also using group=afk does not have the desired effect.
Any ideas how to do this?
reorder should work but the problem is you're trying to re-order by a factor. You need to be explicit on how you want to use that information. You can either use
nl.melt$naam <- reorder(nl.melt$naam, as.numeric(nl.melt$afk))
or
nl.melt$naam <- reorder(nl.melt$naam, as.character(nl.melt$afk), FUN=min)
depending on whether you want to sort by the existing levels of afk or if you want to sort alphabetically by the levels of afk.
After running that and re-running the ggplot code, i get
An alternative to #MrFlick's approach (based on the answer #CarlosCinelli linked to) is:
ggplot(nl.melt, aes(x=interaction(naam,afk), y=perc, fill=stemmen)) +
geom_bar(stat="identity", width=.7) +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
coord_flip() +
theme_bw()
which gives:
R tends to see the order of levels as a property of the data rather than a property of the graph. Try reordering the data itself before calling the plotting commands. Try running:
nl.melt$naam <- reorder(nl.melt$naam, nl.melt$afk)
Then run your ggplot code. Or use other ways of reordering your factor levels in naam.

ggplot2: plotting bars when using stat_summary()

Here is my current script and the output:
ggplot(data.and.factors.prov,aes(x=assumptions,y=FP,
colour=factor(Design.Complexity))) +
stat_summary(fun.data=mean_cl_normal,position=position_dodge(width=0.5)) +
geom_blank() + scale_colour_manual(values=1:7,name='Design Complexity') +
coord_flip()
How can I have (horizontal) bars (starting at FP=0 and ending at the point position) instead of points ? (I don't want to lose the error bars)
I'd like to give you my data.and.factors.prov data.table but it is too big to be posted ! If you need a reproducible example, please let me know how I can give you my data set ?!
For the stat_summary() default geom is "pointrange". To get the bars and errorbars one solution is to use two stat_summary() calls - one to make errorbars and second to calculate just mean values and plot bars. You will need also to adjust width= inside the position_dodge() and fill= to the same factor as for colour= to change filling of bars.
Here is an example with mtcars data.
ggplot(mtcars,aes(x=factor(cyl),y=mpg,colour=factor(gear),fill=factor(gear))) +
stat_summary(fun.data=mean_cl_normal,position=position_dodge(0.95),geom="errorbar") +
stat_summary(fun.y=mean,position=position_dodge(width=0.95),geom="bar")+
coord_flip()

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()

Trying to keep filled bars in a faceted plot

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).

Resources