I'm trying to create a plot with geom_sina and geom_violin where all data points are plotted together (as one violin shape) and are coloured by a factor.
However, when I specify ggplot(mtcars, aes(x = "", y = mpg, fill = am)), the plot is split according to the factor, which is what I'd like to avoid (plot 1). The closest I've come is treating the factor as a continuous variable (plot 2). But then the legend displays a "fill" bar and not the discrete factor levels I'd like.
So, if possible, I'd like the plot to stop splitting by colour when using a factor, or to overide the legend to discrete values if going with numerics.
Any help is much appreciated : )
plot 1
plot 2
Maybe this is what you are looking for. Using the group aesthetic you could overwrite the default grouping by fill or color or ...:
Note: As you want the points do be colored I switched to the color aesthetic.
library(ggplot2)
library(ggforce)
ggplot(mtcars, aes(x = "", y = mpg)) +
geom_violin() +
geom_sina(aes(color = factor(am), group = 1))
Related
I want to plot 3 regression lines, one for each value of tau, and each one with its colour, as specified in the dataset. The internet says you provide the color variable at the general plot aesthetics (http://ggplot.yhathq.com/docs/geom_line.html), but that does not seem to work. Any body would konw what to do?
Thanks in advance,
here is the dataframe
https://www.dropbox.com/s/fqmayg6asf5e3eu/tautest.csv?dl=0
here is the code
ddl=read.csv("tautest.csv")
ggplot(ddl, aes(x = predictor, y = value, color=col)) +
geom_line(aes(group = tau),size = 0.8)
ggplot2 gives some colors, but I want the colors in my dataframe, why would ggplot2 give other colors?
ggplot 'maps' aesthetics to scales, meaning that it tries to fit a variable onto a scale. The default scale for categorical variables is scale_colour_discrete(), which matches a colour to every unique entry. Since ggplot doesn't know that the values in your col column are colour names, you have to let ggplot know that these colours are to be interpreted literally. You can do that with scale_colour_identity().
ggplot(ddl, aes(x = predictor, y = value, color = col)) +
geom_line(aes(group = tau),size = 0.8) +
scale_colour_identity()
I have a time-series, with each point having a time, a value and a group he's part of. I am trying to plot it with time on x axis and value on y axes with the line appearing a different color depending on the group.
I tried using geom_path and geom_line, but they end up linking points to points within groups. I found out that when I use a continuous variable for the groups, I have a normal line; however when I use a factor or a categorical variable, I have the link problem.
Here is a reproducible example that is what I would like:
df = data.frame(time = c(1,2,3,4,5,6,7,8,9,10), value = c(5,4,9,3,8,2,5,8,7,1), group = c(1,2,2,2,1,1,2,2,2,2))
ggplot(df, aes(time, value, color = group)) + geom_line()
And here is a reproducible example that is what I have:
df = data.frame(time = c(1,2,3,4,5,6,7,8,9,10), value = c(5,4,9,3,8,2,5,8,7,1), group = c("apple","pear","pear","pear","apple","apple","pear","pear","pear","pear"))
ggplot(df, aes(time, value, color = group)) + geom_line()
So the first example works well, but 1/ it adds a few lines to change the legend to have the labels I want, 2/ out of curiosity I would like to know if I missed something.
Is there any option in ggplot I could use to have the behavior I expect, or is it an internal constraint?
As pointed by Richard Telford and Carles Sans Fuentes, adding group = 1 within the ggplot aesthetic makes the job. So the normal code should be:
ggplot(df, aes(time, value, color = group, group = 1)) + geom_line()
Probably a simple ggplot2 question.
I have a data.frame with a numeric value, a categorical (factor) value, and a character value:
library(dplyr)
set.seed(1)
df <- data.frame(log10.p.value=c(-2.5,-2.5,-2.5,-2.39,-2,-1.85,-1.6,-1.3,-1.3,-1),
direction=sample(c("up","down"),10,replace = T),
label=paste0("label",1:10),stringsAsFactors = F) %>% dplyr::arrange(log10.p.value)
df$direction <- factor(df$direction,levels=c("up","down"))
I want to plot these data as a barplot using geom_bar, where the bars are horizontal and their lengths are determined by df$log10.p.value, their color by df$direction, and the y-axis tick labels are df$label, where the bars are vertically ordered by df$log10.p.value.
As you can see df$log10.p.value are not unique, hence:
ggplot(df,aes(log10.p.value))+geom_bar(aes(fill=direction))+theme_minimal()+coord_flip()+ylab("log10(p-value)")+xlab("")
Gives me:
How do I:
Make the bars not overlap each other.
Have the same width.
Be separated by a small margin?
Have the y-axis tick labels be df$label?
Thanks
Here is one possible solution. Please note that, by default, geom_bar determines the bar length using frequency/count. So, you need to specify stat = "identity" for value mapping.
# since all of your values are negative the graph is on the left side
ggplot(df, aes(x = label, y = log10.p.value, fill = direction)) +
geom_bar(stat = "identity") +
theme_minimal() +
coord_flip() +
ylab("log10(p-value)") +
xlab("")
I have a categorical axis where i'd like to visually separate groups within that categorical variable. I don't want to facet because it takes up too much space and is visually not as clean.
Here's a visual example of what I want that involves some tedious hacking (setting alpha to 0 for non-data entries used for spacing).
library(ggplot2)
dd <- data.frame(x=factor(c(1,-1,2:10),levels=c(1,-1,2:10)), y=c(1,2,2:10), hidden=as.factor(c(0,1,rep(0,9))))
ggplot(data=dd,aes(x=x,y=y,alpha=hidden)) +
geom_point() + scale_alpha_manual(values=c("1"=0,"0"=1)) +
scale_x_discrete(breaks=c(1:10))
I'd like to be able create this plot without having to hack an extra category in (which wouldn't be feasible with the amount of data/number of groups I'm trying to plot) using the following data structure (where the variable "groups" determines where the spacing occurs):
dd2 <- data.frame(x=factor(1:10,), y=c(1:10), groups=c("A",rep("B",9)))
You can get the result you are looking for via the breaks and limits arguments to scale_x_discrete. Set the breaks to the levels of the factor on the x-axis and the limits to the factor levels with spacers were you want/need them.
Here is an example:
library(ggplot2)
dd <- data.frame(x = factor(letters[1:10]), y = 1:10)
ggplot(dd) +
aes(x = x, y = y) +
geom_point() +
scale_x_discrete(breaks = levels(dd$x),
limits = c(levels(dd$x)[1], "skip", levels(dd$x)[-1]))
Edit: This question is not a duplicate of How to draw stacked bars in ggplot2 that show percentages based on group? . The data there is not purely factors and includes a "y =" in
ggplot(df, aes(x = factor(year), y = amount, fill = type))
As far as I know, "y =" is not relevant to my example.
That said, my goal is to display the percentages for the factor used in fill within ggplot. This is a bit different than other questions because I don't want to display the percentage for the x-variable—I want the percentages to be based on the fill variable, which will be a factor. It's probably easiest to illustrate with mtcars.
df<-data(mtcars)
ggplot(mtcars, aes(x = factor(mtcars$vs),fill=factor(mtcars$am))) +
geom_bar(aes(y=(..count..)/sum(..count..))) +
scale_y_continuous(labels=percent)
This gives us:
What I want, however, is