I'm having difficulty getting geom_jitter to separate my data points by a third variable (phase).
I'd like for each bar in this 2x2 graph to have its own set of jittered data points for each phase condition.
Right now the points are grouped between the two phase bars (sorry couldn't upload the image -- don't have my 10 posts yet).
I know I'm missing something in the geom_jitter aes, but not sure what it is -- fill=phase is not doing the trick.
ggplot(datalong, aes(grp, score)) +
geom_bar(stat = "summary", fun.y = "mean", aes(fill=phase), position = "dodge", alpha = .5) +
geom_jitter(inherit.aes = FALSE, aes(grp, score, fill=phase), position = position_jitter(0.2))
The default shape for points does not take the fill aesthetic
Compare
library(ggplot2)
ggplot(mtcars, aes(mpg, disp, fill = gear)) +
geom_jitter()
ggplot(mtcars, aes(mpg, disp, color = gear)) +
geom_jitter()
ggplot(mtcars, aes(mpg, disp, fill = gear)) +
geom_jitter(shape = 21)
Created on 2020-07-09 by the reprex package (v0.3.0)
Found a solution using:
geom_point(position=position_jitterdodge())
This seems to jitter and dodge to separate the points.
Related
If the labels in the example below were to be overlapping, how could we implement a repel option? Thanks!
means <- df %>%
group_by(cyl) %>%
summarize(across(c(wt, mpg), mean))
ggplot(df) +
aes(x=wt, y=mpg, color=cyl, shape=cyl) +
geom_point() +
geom_point(size=4, data=means) +
geom_label(aes(label=cyl), color="black", data=means) -> fig
fig
If I add the geom_label_repel() from the ggrepel package
fig + geom_label_repel()
I get the error:
geom_label_repel requires the gollowing missing aesthetics: label
You need to map label so that geom_label_repel "sees" it. It doesn't have direct view of the mapping from other geoms. Just itself and the top ggplot call. You thus have two options.
Directly within the function
geom_label_repel(mapping = aes(label = cyl))
or in the top ggplot call
ggplot(data = df, mapping = aes(label = cyl)) +
Note that you'll probably have to specify data as Vincent mentioned in the comment if you want to label the means points.
Is it possible to fill ggplot's geom_dotplot with continuous variables?
library(ggplot2)
ggplot(mtcars, aes(x = mpg, fill = disp)) +
geom_dotplot()
this should be pretty straightforward, but I've tried messing with the groups aes and no success.
The max I can do is to discretize the disp variable but it is not optimal.
ggplot(mtcars, aes(x = mpg, fill = factor(disp))) +
geom_dotplot()
Good question! You have to set group = variable within aes (where variable is equal to the same column that you're using for fill or color):
library(ggplot2)
ggplot(mtcars, aes(mpg, fill = disp, group = disp)) +
geom_dotplot()
geom_dotplot in away is just like a histogram. You can't set fill/colour there easily as grouping is done. To make it work you have to set group.
Example using geom_histogram:
ggplot(mtcars, aes(mpg, fill = disp, group = disp)) +
geom_histogram()
In the below graph, I want to change the colour of the left graph as red, and the right graph as blue. How can I simply do that using the below command? Many thanks in advance.
normgraph <- ggplot (data=data1, aes (x=x1)) +
geom_density() +
geom_density(data=data2, aes(x=x2)) +
labs(title="The variation of grain weight") +
labs (y="Frequency(%)") +
labs (x="Grain weight (mg)") +
lims(x=c(50,70))
Since data is not provided, I will use iris to illustrate how to do this. Note that code is similar to yours; I just haven't included any further customizations relevant for your plot (as they depend on your dataset).
library(ggplot2)
ggplot(data = iris, aes(x = Sepal.Length)) +
geom_density(color = "blue") +
geom_density(data = iris, aes(x = Sepal.Width), color = "red")
Created on 2019-02-13 by the reprex package (v0.2.1)
I am trying to plot some boxplots as semi-transparent. When I set the alpha value, only the fill transparency is adjusted, and not the borders/strokes/colors.
Any idea how to make the whole geom layer transparent?
library(ggplot2)
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot(aes(fill = factor(cyl), color = factor(cyl)), alpha = 0)
This won't work out of the box, since for polygons ggplot2 only applies alpha to fill, not colour. In order to fix that, we'll apply the following ad-hoc patch by taking low-level internals and adding alpha mapping where needed.
Check out the following gist. Won't post it here, as it is too lengthy.
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot(aes(fill = factor(cyl), color = factor(cyl)), alpha = 0.4, size = 1.4)
Before:
After:
I am making a boxplot conditioned by a factor similar to this example:
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(aes(fill = factor(am)))
There are few points in the data set, and I'd like to express this visually by overlaying the data points. I want to overlay the points colored by the same factor "am" which I try to do like this:
p + geom_boxplot(aes(fill = factor(am))) + geom_jitter(aes(colour = factor(am)))
The points are colored by the factor "am" but not spaced to lay only over the box plots they are associated with. Rather they mix and cover both.
Does anyone know how the condition the geom_jitter so the points associate with the factor "am"?
Welcome to SO! Here's my attempt. It's a bit clumsy, but does the job. The trick is to map x to a dummy variable with manually constructed offset. I'm adding a fill scale to highlight point positioning.
mtcars$cylpt <- as.numeric(factor(mtcars$cyl)) + ifelse(mtcars$am == 0, -0.2, 0.2)
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot(aes(fill = factor(am))) +
geom_point(aes(x = cylpt, colour = factor(am)), position = "jitter") +
scale_fill_manual(values = c("white", "gray"))
I have found this link that solves your problem:
https://datavizpyr.com/how-to-make-grouped-boxplot-with-jittered-data-points-in-ggplot2/
geom_jitter(position = position_jitterdodge())