I make some plot with ggplot2 like this:
library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
geom_point()
Everything is good but as soon I set another legend title as shown here, the continuous colour gradient changes to some points in the legend:
library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
geom_point() +
guides(col= guide_legend(title= "Some title"))
How can I change just the title not the eastethic in the legend?
Data used here:
df <- data.frame(x= 1:10, y= 1:10, col= rnorm(10))
You need guide_colourbar instead of guide_legend
ggplot(df, aes(x, y, col = col)) +
geom_point() +
guides(col = guide_colourbar(title = "Some title"))
Though personally, I would normally just change the label of the color aesthetic:
ggplot(df, aes(x, y, col = col)) +
geom_point() +
labs(color = "Some title")
Which gives the same result with fewer keystrokes.
Related
I'm using ggMarginal to make marginal boxplots. Is there a way to manually change the color and/or fill of the boxplots without a grouping variable? I'd like to have different colors on the x boxplot and the y boxplot.
library(tidyverse)
library(ggExtra)
foo <- data.frame(x=rnorm(100,mean=1,sd=1),
y=rnorm(100,mean=2,sd=2))
p1 <- ggplot(data = foo,aes(x=x,y=y)) +
geom_point() + coord_equal()
ggMarginal(p1, type="boxplot", size=12)
Provided I have understood you correctly, you can do the following
p1 <- ggplot(data = foo, aes(x = x, y = y)) +
geom_point() +
coord_equal()
ggMarginal(
p1,
type = "boxplot",
size = 12,
xparams = list(colour = "blue"),
yparams = list(colour = "red"))
I want to overlay two plots: one is a simple point plot where a variable is used to control the dot size; and another is a simple curve.
Here is a dummy example for the first plot;
library(ggplot2)
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla")
Now lets overlay a curve to this plot with data from another dataframe:
df2 = data.frame(x=x, y=x^2-x+2)
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla") + geom_line(data=df2, aes(x, y), color = "blue") + scale_color_discrete(name = "other", labels = c("nanana"))
It produces the error:
Error in FUN(X[[i]], ...) : object 'v' not found
The value in v is not used to draw the intended curse, but anyway, I added a dummy v to df2.
df2 = data.frame(x=x, y=x^2-x+2, v=replicate(length(x),0)) # add a dummy v
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla") + geom_line(data=df2, aes(x, y), color = "blue") + scale_color_discrete(name = "other", labels = c("nanana"))
An the result has a messed legend:
What is the right way to achieve the desired plot?
You can put the size aes in the geom_point() call to make it so that you don't need the dummy v in df2.
Not sure exactly what you want regarding the legend. If you replace the above, then the blue portion goes away. If you want to have a legend for the line color, then you have to place color inside the geom_line aes call.
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
df2 = data.frame(x=x, y=x^2-x+2)
ggplot(df, aes(x, y)) +
geom_point(aes(size = v)) +
theme_classic() +
scale_size("blabla") +
geom_line(data=df2, aes(x, y, color = "blue")) +
scale_color_manual(values = "blue", labels = "nanana", name = "other")
I'd like to add a line below the x-axis where its color is dependant on a factor that is not plotted.
In this example, I'm creating a box plot and would like to add a line that indicates another variable.
Using the cars data set as an example and then physically dawing in what I'm trying to do:
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot()
My thought was to create a bar, column, or geom_tile plot and then arrange it below the boxplot. This is how I would do it in base R. Is there a way to add in these kinds of color labels in ggplot2?
The natural way in ggplot2 to do this sort of thing would to be facet on the categorical variable to create subplots. However if you want to keep everything on the same graph you could try using a geom_tile() layer something like this:
df <-data.frame(x = factor(c(4,6,8)), colour = factor(c(1,2,1)))
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 8, fill = colour))
Alternatively as you suggest you could align an additional plot underneath it. You could use ggarrange() in the ggpubr package for this:
plot1 <- ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 10, fill = colour))
theme(legend.position = 'none')
plot2 <- ggplot(df, aes(x=x, y=1, fill = colour)) +
geom_tile() +
theme_void() +
scale_fill_manual(values=c('orange', 'green', 'orange')) +
theme(legend.position = 'none')
library(ggpubr)
ggarrange(plot1, plot2, nrow = 2, heights = c(10, 1), align = 'h')
Here I have two 'clusters', and just one legend.
How can I get two "density" legends with two different color gradients?
I have tried group but it does not work.
The following code generated the above graph:
library(ggplot2)
df <- data.frame(x=c(rnorm(1000,1,.1),rnorm(1000,3,.1)),
y=c(rnorm(1000,1,1),rnorm(1000,3,1)),
type=c(rep('a',1000),rep('b',1000)))
plot( ggplot(df) +
stat_bin2d(aes(x,y,fill=..density..,group='type')))
I'm not aware of a way to specify more than one fill gradient. But here's a work around that uses different transparency levels to simulate the gradient, leaving fill available to be mapped with type:
ggplot(df, aes(x, y, fill = type)) +
stat_bin2d(aes(alpha = ..density..)) +
scale_alpha(range = c(1, 0.1)) +
theme_bw()
Using alpha = ..density.. does the trick:
ggplot(df, aes(x = x, y = y) ) +
stat_bin2d(mapping= aes(alpha = ..density.., fill = type))
A bit more aesthetically using stat_density2d e.g.:
ggplot(df, aes(x=x, y=y) ) +
stat_density2d(mapping= aes(alpha = ..level.., color= type), geom="contour", bins=6, size= 2)
From Plot vectors of different length with ggplot2, I've got my plot with lines.
ggplot(plotData, aes(x, y, label=label, group=label)) + geom_line() + stat_smooth()
But this smooths one line each. How do I smooth over all data points?
ggplot(plotData, aes(x, y, label=label, group=label)) +
geom_line() +
geom_smooth(aes(group = 1))
should do it. The idea here is to provide a new group aesthetic so that the fitted smoother is based on all the data, not the group = label aesthetic.
Following the example from #Andrie's Answer the modification I propose would be:
ggplot(plotData, aes(x, y, label=label, group=label)) +
geom_text() +
geom_smooth(aes(group = 1))
which would produce: