I'm trying to plot a multiple group histogram with overlaid line, but I cannot get the right scaling for the histogram.
For example:
ggplot() + geom_histogram(data=df8,aes(x=log(Y),y=..density..),binwidth=0.15,colour='black') +
geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw()
produces the right scale. But when I try to perform fill according to groups, each group is scaled separately.
ggplot() + geom_histogram(data=df8,aes(x=log(Y),fill=vec8,y=..density..),binwidth=0.15,colour='black') +
geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw()
How would I scale it so that a black line is overlaid over the histogram and on the y axis is density?
It is going to be difficult for others to help you without a reproducible example, but perhaps something like this is what you're after:
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg, fill = factor(cyl))) +
geom_histogram(aes(y = ..density..)) +
geom_line(stat = "density")
If you would rather the density line pertain to the entire dataset, you need to move the fill aesthetic into the geom_histogram function:
ggplot(data = mtcars, aes(x = mpg)) +
geom_histogram(aes(y = ..density.., fill = factor(cyl))) +
geom_line(data = mtcars, stat = "density")
Related
I am interested in doing a plot showing percentages by group.
something like this:
data(iris)
ggplot(iris,
aes(x = Sepal.Length, group = factor(Species), fill = factor(Species))) +
geom_histogram(position = "fill")+theme_bw()
however, I would also like to plot a histogram showing the frequency distribution on top of this graph.
something like the plot below.
ggplot(iris,aes(x = Sepal.Length)) +
geom_histogram()+theme_bw()
Does anyone know how to do this?
Note I know how to do a frequency plot by group: ggplot(iris,aes(x = Sepal.Length, group = factor(Species), fill = factor(Species))) + geom_histogram()+theme_bw(). But this is not what I want. Rather I would like a small frequency distribution at the bottom of the percentage plot presented at the beginning.
Thank you very much
Something like this?
library(gridExtra)
p1 <- ggplot(iris,
aes(x = Sepal.Length,
group = factor(Species),
fill = factor(Species))) +
geom_histogram(position = "fill") +
theme_bw() +
theme(legend.position = "top")
p2 <- ggplot(iris,aes(x = Sepal.Length,
group = factor(Species),
fill = factor(Species))) +
geom_histogram() +
theme_bw() +
theme(legend.position = "none")
grid.arrange(p1, p2,
heights = c(4, 1.5))
Edit: So you are looking for this then? Note that in this case the absolute values of the smaller histogram become meaningless since they were scaled down to be ~25% of the vertical chart range.
ggplot() +
geom_histogram(data = iris,
aes(x = Sepal.Length,
group = factor(Species),
fill = factor(Species)),
position = "fill",
alpha = 1) +
geom_histogram(data = iris,
aes(x = Sepal.Length,
y = ..ncount.. / 4),
alpha = 0.5,
fill = 'black')
I maked the chart in R using gglopt() and facet_warp(), but do not appear legends of geom_lines() and stat_smooth().
my code exemple is:
p <- ggplot(data = mtcars, aes(x = hp, y = disp)) +
geom_line(color="red")+
facet_wrap(~cyl)+
stat_smooth()+
guides()
how to add legends in the chart final?
You can add the labels for color aesthetics for each plot and link the color using named vectors in values parameter of scale_color_manual().
ggplot(data = mtcars, aes(x = hp, y = disp)) +
geom_line(aes(color = "line.color")) +
stat_smooth(aes(color = "smooth.color")) +
facet_wrap(~cyl) +
scale_color_manual(name = "", values = c("line.color" = "red", "smooth.color" = "blue"))
I'm creating a table with a lot of plot using ggplot
a=rnorm(30)
b=a*a
c=rnorm(30)
d=c
l=runif(30)
m=l+3
data=data.frame(A=a,B=b,ss=1)
data=rbind(data,data.frame(A=c,B=d,ss=2))
ggplot()+ geom_line(data=data,aes(A,B,group=ss),col="red")+facet_wrap(~ ss,as.table=T
In each of this plots I have to overlap an histogram.
How can I do?
Here's a way to do it:
ggplot() +
geom_line(data = data, aes(x = A, y = B), col = "red") +
geom_histogram(data = data, aes(x = A), alpha = .5) +
facet_wrap(~ ss,as.table=T)
In the lattice package of R it is possible to create stacked bar charts. I would like to have several stacked bars side by side similar to this one:
barchart( mpg ~ as.factor(gear), data=mtcars, groups=cyl, stack=F, horizontal=F, auto.key=T )
This is almost what I need. The problem is that the bars are layered, e.g. for the pink bar at the center there are 3 layered bars of approximately the same value (between 17 and 22). The bars are not stacked. The bar that is painted later covers bars painted earlier.
Would it also be possible to have different colors/textures for the stacked bars as well as for the side-by-side bars and an additional legend? The different levels in the stack come from an additional factor.
library(ggplot2)
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) + geom_bar(stat = "identity", colour = "black") + facet_wrap(~gear)
mtcars$ID <- rownames(mtcars)
ggplot(mtcars, aes(x = factor(gear), y = mpg, fill = factor(cyl), group = ID)) + geom_bar(stat = "identity", position = "dodge")
ggplot(mtcars, aes(x = factor(gear), y = mpg, colour = factor(cyl))) + geom_jitter()
ggplot(mtcars, aes(x = factor(gear), y = mpg, colour = factor(cyl))) + geom_boxplot()
I plotted two ggplots from two different datasets in one single plot. plots are simple linear regression. I want to add legend both for lines and dots in the plot with different colours. How can I do that? The code I used for plot is as below. But, I failed to add a desirable legend to that.
ggplot() +
geom_point(aes(x = Time_1, y = value1)) +
geom_point(aes(x = Time_2, y = value2)) +
geom_line(aes(x = Time_1, y = predict(reg, newdata = dataset)))+
geom_line(aes(x = Time_Month.x, y = predict(regressor, newdata = training_set)))+
ggtitle('Two plots in a single plot')
ggplot2 adds legends automatically if it has groups within the data. Your original code provides the minimum amount of information to ggplot(), basically enough for it to work but not enough to create a legend.
Since your data comes from two different objects due to the two different regressions, then it looks like all you need in this case is to add the 'color = "INSERT COLOR NAME"' argument to each geom_point() and each geom_line(). Using R's built-in mtcars data set for example, what you have is similar to
ggplot(mtcars) + geom_point(aes(x = cyl, y = mpg)) + geom_point(aes(x = cyl, y = wt)) + ggtitle("Example Graph")
Graph without Legend
And what you want can be obtained by using something similar to,
ggplot(mtcars) + geom_point(aes(x = cyl, y = mpg, color = "blue")) + geom_point(aes(x = cyl, y = wt, color = "green")) + ggtitle("Example Graph")
Graph with Legend
Which would seem to translate to
ggplot() +
geom_point(aes(x = Time_1, y = value1, color = "blue")) +
geom_point(aes(x = Time_2, y = value2, color = "green")) +
geom_line(aes(x = Time_1, y = predict(reg, newdata = dataset), color = "red"))+
geom_line(aes(x = Time_Month.x, y = predict(regressor, newdata = training_set), color = "yellow"))+
ggtitle('Two plots in a single plot')
You could also use the size, shape, or alpha arguments inside of aes() to differentiate the different series.