I'm trying to create a stacked density graph in ggplot2, and I am also trying to understand how qplot works relative to ggplot.
I found the following example online:
qplot(depth, ..density.., data=diamonds, geom="density",
fill=cut, position="stack")
I tried translating this into a call to ggplot because I want to understand how it works:
ggplot(diamonds, aes(x=depth, y=..density..)) +
geom_density(aes(fill=cut, position="stack"))
This creates a density graph, but does not stack it.
What is the different between what qplot is creating and what ggplot is creating?
Here is a stacked density graph:
Non-stacked density graph:
Original example is here
From #kohske's comment, the position is not an aesthetic, and so should not be inside the aes call:
ggplot(diamonds, aes(x=depth, y=..density..)) +
geom_density(aes(fill=cut), position="stack")
or using the movies data (which your example graphs use):
ggplot(movies, aes(x=rating, y=..density..)) +
geom_density(aes(fill=mpaa), position="stack")
Related
I have a boxplot output in R using ggplot2. box plot i got using the below code
I want to label each box plot as labelled in the sample plot. sample plot i want to get
I have calculated p-value that is 0.06 for first egg1. i would like to paste this text on the plot as shown in the sample plot. how i can do that?
ggplot(testdata) +
geom_boxplot(aes(x=variable, y=value, color= as.factor (classification)))
You can use annotate to add text on your boxplot:
ggplot(testdata) +
geom_boxplot(aes(x=variable, y=value, color= as.factor (classification))) +
annotate(geom="text", x=1, y=6, label="p = 0.06")
I have couple of questions regarding plotting using ggplot2.
I have already used below commands to colour data points using R.
library(ggplot2)
df <- read.csv(file="c:\\query2.csv")
ggplot( df,aes( x = Time,y ,y = users,colour = users>40) ) + geom_point()
My question is: how should I draw a continuous line connecting data points and how do I circle around data points for users >40?
To connect the points, use geom_line (if that doesn't give you what you need, please explain what you're trying to accomplish).
I haven't used geom_encircle, but another option is to use a filled marker with the fill deleted to create the circles. Here's an example, using the built-in mtcars data frame for illustration:
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_point(data=mtcars[mtcars$mpg>30,],
pch=21, fill=NA, size=4, colour="red", stroke=1) +
theme_bw()
pch=21 is one of the filled markers (see ?pch for more info on other available point markers). We set fill=NA to remove the fill. stroke sets the thickness of the circle border.
UPDATE: To add a line to this chart, using the example above:
ggplot(mtcars, aes(wt, mpg)) +
geom_line() +
geom_point() +
geom_point(data=mtcars[mtcars$mpg>30,],
pch=21, fill=NA, size=4, colour="red", stroke=1) +
theme_bw()
However, if (as in my original code for this graph) you put the aes statement inside the geom, rather than in the initial call to ggplot, then you need to include an aes statement inside geom_line as well.
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
I've got something like 20 facets on a geom_line ggplot2 plot, with an overlaid geom_rect based on timeseries data, all with a facet_wrap. I constantly need to update my plots, and the order of my facets ultimately may need to change on a daily basis.
My question is: is it possible to order my facets using my time-series data in geom_rect? I.e. make the first facet the one that has the first geom_rect shaded area, and so on and so forth?
Here is my code: x-axis is date, y-axis is incidence3, and faceted by geo....
ggplot () +
geom_rect(data=total,
aes(xmin=as.Date(xmin),
xmax=as.Date(xmax),
ymin=-Inf,
ymax=Inf),
fill='light blue',
alpha=0.3) +
ylab("incidence") + xlab("time") +
facet_wrap(~geo) +
geom_line(data=total, aes(x=as.Date(date), y=incidence3)) +
facet_wrap(~geo, ncol=2, scale = "free_y")
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')