I needed to reinstall R and I now encounter a little problem with ggplot. I am sure there is a simple solution to it and I appreciate all hints!
I am using the stacked area plot quite often, and usually I got the desired stacking and legend order by defining the factor levels and plotting in reverse order. However, this is not working any more after the re-installation.
Here is an example:
dx <- data.frame(x=rep(1:8,3),y=rep(c(2,3,2,4,3,5,3,2),3),z=c(rep("bread",8),rep("butter",8),rep("fish",8)))
ggplot() + geom_area(data=dx, aes(x=x, y=y, fill=z, order=-as.numeric(z)))
This gives the following plot:
It looks as if "order" did not have any impact on the plot.
The desired plot would stack the areas as shown in the legend, i.e. red area on top, blue area at the bottom.
Where is my mistake?
Many thanks in advance!
You can either use (the colors will also be reversed):
dx$z <- factor(dx$z, levels = rev(levels(dx$z)))
ggplot() + geom_area(data=dx, aes(x=x, y=y, fill=z))
Or directly use this (without reversing the factor levels, which won't change the color):
ggplot() + geom_area(data=dx, aes(x=x, y=y, fill=z)) +
guides(fill = guide_legend(reverse=TRUE))
Related
I am trying to make a manual colour scale for my bar graph using plyr to summarize the data and ggplot2 to present the graph.
The data has two variables:
Region (displayed on the X-axis)
Genotype (displayed by the fill)
I have managed to do this already, however, I have not been able to find a way to personalize the colours - it simply gives me two randomly assigned colours.
Could someone please help me figure out what I am missing here?
I have included my code and an image of the graph below. The graph basically has the appearance I want it to, except that I can't personalize the colours.
ggplotdata <- summarySE(data, measurevar="Density", groupvars=c("Genotype", "Region"))
ggplotdata
#Plot the data
ggplotdata$Genotype <- factor(ggplotdata$Genotype, c("WT","KO"))
Mygraph <-ggplot(ggplotdata, aes(x=Region, y=Density, fill=Genotype)) +
geom_bar(position=position_dodge(), stat="identity",
colour="black",
size=.2) +
geom_errorbar(aes(ymin=Density-se, ymax=Density+se),
width=.2,
position=position_dodge(.9)) +
xlab(NULL) +
ylab("Density (cells/mm2)") +
scale_colour_manual(name=NULL,
breaks=c("KO", "WT"),
labels=c("KO", "WT"),
values=c("#FFFFFF", "#3366FF")) +
ggtitle("X") +
scale_y_continuous(breaks=0:17*500) +
theme_minimal()
Mygraph
The answer here was to use scale_fill_manual instead, thank you #dc37
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
Basics:
Using R statistical software, ggplot2, geom_vline, and geom_histogram to visualize some data. The issue is with the legend keys.
I'm trying to plot a pair of histograms from some stochastic simulations, and on top of that plot a couple of lines representing the result of a deterministic simulation. I've got the data plotted, but the legend keys for the histograms have an unnecessary black line through the middle of them. Can you help me remove those black lines? Some sample code reproducing the issue is here:
df1 <- data.frame(cond = factor( rep(c("A","B"), each=200) ),
rating = c(rnorm(200),rnorm(200, mean=.8)))
df2 <- data.frame(x=c(.5,1),cond=factor(c("A","B")))
ggplot(df1, aes(x=rating, fill=cond)) +
geom_histogram(binwidth=.5, position="dodge") +
geom_vline(data=df2,aes(xintercept=x,linetype=factor(cond)),
show_guide=TRUE) +
labs(fill='Stochastic',linetype='Deterministic')
Edit: added image
Cheers,
Ryan
One workaround is to change the order of geom_histogram() and geom_vline(). Then add another geom_vline() without aes(), just giving xintercept= and linetype=. This will not remove lines but will hide them under the color legend entries.
ggplot(data=df1, aes(x=rating, fill=cond)) +
geom_vline(data=df2,aes(xintercept=x,linetype=factor(cond)),
show_guide=TRUE) +
geom_histogram(binwidth=.5, position="dodge") +
geom_vline(xintercep=df2$x,linetype=c(1,3))+
labs(fill='Stochastic',linetype='Deterministic')
Maybe it's because of the dark outside, but I can't get this
Position geom_text on dodged barplot
to work on my fairly simple dataframe
fs <- data.frame(productcategory=c("c2","c2"), product=c("p4", "p5"), ms1=c(2,1))
plot <- ggplot(data=NULL)
plot +
geom_bar(data=fs, aes(x=productcategory, y=ms1, weight=ms1, fill=product),stat="identity", position="dodge") +
geom_text(data=fs, aes(label = ms1, x = productcategory, y=ms1+0.2), position=position_dodge(width=1)))
My plot still shows the labels in the "middle" of the product category and not above of the proper product.
Looks like this even it seems very simple, but I'm totally stuck on this
So any hints are very much appreciated how to get labels above the proper bars.
Tom
Because you have the aesthetics defined for each geom individually, geom_text isn't picking up on the fact that you're subdividing the x variable productcategory by the fill variable product.
You can get the graph you want by adding fill=product to the aes() call for geom_text, or you can try to define as many aesthetics as possible in the original ggplot() call, so that all the geoms pick up on those aesthetics automatically and you only have to define them if they're specific to that particular geom.
plot2 <- ggplot(data=fs, aes(x=productcategory, y=ms1, fill=product)) +
geom_bar(stat="identity", position="dodge") +
geom_text(aes(label=ms1, y =ms1 + 0.2), position=position_dodge(width=1))
print(plot2)
Intuitively I'm looking for something like: facet_(scales="free_color")
I do something like
p <- ggplot(mpg, aes(year, displ, color=model)) + facet_wrap(~manufacturer)
p + geom_jitter()
That is: plot 2d measurements from individuals(model) belonging to different species(manufacturer) faceted by a species, indicating the individual by color.
The problem is that all individuals share the same color scale - so that the points in a facet have very similar colors.
Using the group aesthetic with geom_line would solve the problem, but lines tell different story than dots.
Another obvious solution would be to drop the faceting and draw a separate plot for each subset. (If this should be the only solution: are there any quick, smart or proven ways to do that?)
I'm not sure that this is an available option when you're colouring by a factor. However, a quick way to produce the individual plots would be something like this:
d_ply(mpg, .(manufacturer), function(df) {
jpeg(paste(df$manufacturer[[1]], ".jpeg", sep=""))
plots <- ggplot(df, aes(year, displ, color=factor(model))) + geom_jitter()
print(plots)
dev.off()
})
Related Answers:
Different legends and fill colours for facetted ggplot?
I think you simply want to color by class, where each manufacturer makes several models, each only one or two per class:
p <- ggplot(mpg, aes(year, displ, color=class)) + facet_wrap(~ manufacturer)
p + geom_jitter()