I wonder how it's working to set aesthetics in ggplot. How it's possible to know where to put the aes in ggplot?
Consider this code:
p <- ggplot(data = mtcars,
mapping = aes(x = wt,
y = mpg,
colour = "blue"))
# A basic scatter plot
hello = p + geom_point(size = 4) + ggtitle(label = 'Hello')
goodbye = p + geom_point(aes(colour = factor(cyl)), size = 4) + ggtitle(label = 'Goodbye')
col.points = p + geom_point(size = 4, color = "blue") + ggtitle(label = 'Col.points')
gridExtra::grid.arrange(hello, goodbye, col.points)
Here, it's possible to see that the colour "blue" is not applied to the points in the first graph (hello), and in the second (goodbye) it's getting the colour from a column. But what's the difference? The col.points example shows that the points are indeed coloured.
The difference is that when the aes are set in the original ggplot, they are inherited by any other geom's that build on top of it. If you specify the aes only in a geom, it will only be used in that geom. If you use any specific aes in geom, they override the settings in ggplot.
In your example code, in the first instance:
p + geom_point(size = 4)
The size of the points is set to 4, and the aes(wt, mp, colour = 'red') is inherited from ggplot. In the second case:
p + geom_point(aes(colour = factor(cyl))
The resulting aes is aes(wt, mpg, colour = factor(cyl) as the wt and mpg are inherited from the ggplot object, and the colour = factor(cyl) overwrites the colour = 'red'.
Related
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 currently getting started learning R and I'm focusing on data visualisation.
For this plot, I'm displaying the count of overlapping dots on the map using geom_count which gives me the following graph
As you can see the legend only contains two elements, namely the size of the dot when 5 data points are overlapping, and the size of it when 10 data points are overlapping. How can I increase the breaks that the legend includes? I have been trying with to use discrete_x_scale in order to increase the number of breaks but I just get lost and can't manage it.
The code for my current graph is simply this
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
geom_count()
I would also like to know how to change the filling color of the dot according to the number of overlapping data points.
You need to modify scale_size, not scale_x:
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
geom_count() +
scale_size(breaks = c(2, 4, 6, 8))
To also change the fill colour, you can use a computed aesthetic:
ggplot(data = mpg, mapping = aes(x = cty, y = hwy, color = after_stat(n))) +
geom_count() +
scale_size(breaks = seq(0, 15, 3)) +
scale_color_continuous(breaks = seq(0, 15, 3)) +
guides(size = guide_legend(), color = guide_legend())
Note the guides call: without that, you’d get two separate legends for the size and colour below each other, rather than one merged legend.
To address the question of changing the fill colour as well as size try by creating an explicit count variable which is used to control size and colour:
library(dplyr)
library(ggplot2)
mpg1 <-
mpg %>%
group_by(cty, hwy) %>%
summarise(count = n())
ggplot(data = mpg1, mapping = aes(x = cty, y = hwy, colour = count, size = count))+
geom_point() +
scale_size_continuous(breaks = seq(2, 14, by = 2))+
scale_colour_continuous(breaks = seq(2, 14, by = 2))+
guides(colour = guide_legend(), size = guide_legend())
Note to ensure that only one legend title appears both the breaks for size and colour need to be identical.
Created on 2021-04-01 by the reprex package (v1.0.0)
A very similar question to the one asked here. However, in that situation the fill parameter for the two plots are different. For my situation the fill parameter is the same for both plots, but I want different color schemes.
I would like to manually change the color in the boxplots and the scatter plots (for example making the boxes white and the points colored).
Example:
require(dplyr)
require(ggplot2)
n<-4*3*10
myvalues<- rexp((n))
days <- ntile(rexp(n),4)
doses <- ntile(rexp(n), 3)
test <- data.frame(values =myvalues,
day = factor(days, levels = unique(days)),
dose = factor(doses, levels = unique(doses)))
p<- ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot( aes(fill = dose))+
geom_point( aes(fill = dose), alpha = 0.4,
position = position_jitterdodge())
produces a plot like this:
Using 'scale_fill_manual()' overwrites the aesthetic on both the boxplot and the scatterplot.
I have found a hack by adding 'colour' to geom_point and then when I use scale_fill_manual() the scatter point colors are not changed:
p<- ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot(aes(fill = dose), outlier.shape = NA)+
geom_point(aes(fill = dose, colour = factor(test$dose)),
position = position_jitterdodge(jitter.width = 0.1))+
scale_fill_manual(values = c('white', 'white', 'white'))
Are there more efficient ways of getting the same result?
You can use group to set the different boxplots. No need to set the fill and then overwrite it:
ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot(aes(group = interaction(day, dose)), outlier.shape = NA)+
geom_point(aes(fill = dose, colour = dose),
position = position_jitterdodge(jitter.width = 0.1))
And you should never use data$column inside aes - just use the bare column. Using data$column will work in simple cases, but will break whenever there are stat layers or facets.
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")
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.