I have currently tried a course and would like to replicate it on my r-studio however, why can I only show a single color bar plot provided the code below? Is it related to the base color of the fill function? Thanks!
head(mtcars)
library(ggplot2)
# Bar chart
ggplot(mtcars, aes(x = cyl, fill = am)) +
geom_bar(position = "fill")
# Convert bar chart to pie chart
ggplot(mtcars, aes(x = factor(1), fill = am)) +
geom_bar(position = "fill") +
facet_grid(. ~ cyl) + # Facets
coord_polar(theta = "y") + # Coordinates
theme_void() # theme
Welcome to SO! I think you only need to add the group option to the aes():
ggplot(mtcars, aes(x = cyl, fill = am, group = am)) + geom_bar(position = "fill")
But maybe in this way it could be more readable:
ggplot(mtcars, aes(x = as.factor(cyl), fill = as.factor(am), group = as.factor(am))) +
geom_bar(position = "fill") +
xlab("CYL") + # change x axis label
labs(fill = "am") # change legend title
Related
I cannot understand why the legend is not changing given the following code. The same options do the trick with geom_histogram. Thanks in advance for any assistance.
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_fill_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))
You used color in your call of aes(). To modify the scale for this variable you need to use scale_color_manual and not scale_fill_manual.
It's tricky because geom_histogram does use fill, but geom_density uses color.
Working solution :
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_color_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))
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 want to shrink the legends distance to the x-axis, but I can't find a way to do it. Is it possible?
Example code:
library(ggplot2)
ggplot(mtcars, aes(x = gear, y = mpg, fill = gear)) +
geom_bar(stat = "identity") +
theme(legend.position = "bottom")
Just adjust the margin value.
ggplot(mtcars, aes(x = gear, y = mpg, fill = gear)) +
geom_bar(stat = "identity") +
theme(legend.position = "bottom",legend.margin=unit(-.05,"cm"))
Ack. The answer given by #AdamBirenbaum doesn't work anymore. legend.margin must be specified using margin() (as the error message reports).
So now the correct answer should be
ggplot(mtcars, aes(x = gear, y = mpg, fill = gear)) +
geom_bar(stat = "identity") +
theme(legend.position = "bottom",legend.margin=margin(t=-10))
See: https://www.rdocumentation.org/packages/ggplot2/versions/3.2.1/topics/theme
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")
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()