How to remove inclined lines added to legend? And also the dots on the yellow and gray... Why is it happening?
library(ggplot2)
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-5*x-10)
ggplot(df, aes(x, y)) +
geom_point(aes(size = v)) +
theme_classic() +
scale_size("blabla") +
geom_point(data=df2, aes(x, y, color = "blue")) +
geom_line(data=df2, aes(x, y, color = "blue")) +
geom_hline(aes(color="gray",yintercept=25)) +
geom_abline(aes(color="yellow", intercept=0, slope=1)) +
scale_color_manual(values = c("blue","gray","yellow"), labels = c("nanana","hhh","abab"), name = "other")
That's the legend for the color aesthetic and it tries to combine all the needed information for geom_point, geom_line, geom_hline, and geom_abline. To get rid of the lines, we instead need
geom_abline(aes(color = "yellow", intercept = 0, slope = 1), show.legend = FALSE)
while for the dots we have to add
guides(color = guide_legend(override.aes = list(shape = c(19, NA, NA))))
This gives
I have similar data like the following example:
dat1 <- data.frame(group=c("a", "a","a", "a","a","a","b","b","b","b","b", "b", "b","b","b","c","c","c","c","c","c"),
subgroup=c(paste0("R", rep(1:6)),paste0("R", rep(1:9)),paste0("R", rep(1:6))),
value=c(15,16,12,12,14,5,14,27,20,23,14,10,20,22,14,15,18,14,23,30,32),
pp=c("AT","BT","CT","AA","CC","SE","DN","AS","MM","XT","QQ","HH","MK","HT","dd","US","AG","TT","ZZ","XK","RU"),
clusters=c(rep("cluster1",6),rep("cluster2",9),rep("cluster3",6)))
colors <- c(rep("#74c1e8",6),rep("#808000",9),rep("#FF69B4",6))
names(colors) <- c("cluster1","cluster2","cluster3")
my code is :
pl <- ggplot(dat1, aes(y = pp, x = subgroup))
+ geom_point(aes(size=value))
+ facet_grid(~group, scales="free_x", space = "free")
+ ylab("names")
+ xlab(" ")
+ theme(axis.text.y = element_text(color=colors))
pl
What I want is to add some space on y_axis after each cluster. For example, after cluster 3 (red ones) I want to add some space like space between panels, etc. in the following plot.
Is there a way to do that?
My solution converts the y axis to a factor and add geom_hline between each cluster
library(tidyverse)
dat1 <- data.frame(group=c("a", "a","a", "a","a","a","b","b","b","b","b", "b", "b","b","b","c","c","c","c","c","c"),
subgroup=c(paste0("R", rep(1:6)),paste0("R", rep(1:9)),paste0("R", rep(1:6))),
value=c(15,16,12,12,14,5,14,27,20,23,14,10,20,22,14,15,18,14,23,30,32),
pp=c("AT","BT","CT","AA","CC","SE","DN","AS","MM","XT","QQ","HH","MK","HT","dd","US","AG","TT","ZZ","XK","RU"),
clusters=c(rep("cluster1",6),rep("cluster2",9),rep("cluster3",6)))
colors <- c(rep("#74c1e8",6),rep("#808000",9),rep("#FF69B4",6))
names(colors) <- c("cluster1","cluster2","cluster3")
ggplot(dat1, aes(y = factor(pp), x = subgroup)) + geom_point(aes(size=value)) + facet_grid(~group, scales="free_x", space = "free")+
ylab("names") +
xlab(" ") +
theme(axis.text.y = element_text(color=colors)) +
geom_hline(yintercept = 15.5, color = "white", size = 2) +
geom_hline(yintercept = 6.5, color = "white", size = 2)
I want to add a legend to my graph. All solutions I found online use scale_color_manual - but it's not working for me. Where is the legend?
Here is my code:
library(ggplot2)
ggplot() +
geom_density(aes(x = rnorm(100)), color = 'red') +
geom_density(aes(x = rnorm(100)), color = 'blue') +
xlab("Age") + ylab("Density") + ggtitle('Age Densities')
theme(legend.position = 'right') +
scale_color_manual(labels = c('first', 'second'), values = c('red', 'blue'))
If for some reason you absolutely need the two geoms to take on different data sources, move the color = XXX portion inside aes() for each, then define the colors manually using a named vector:
ggplot() +
geom_density(aes(x = rnorm(100), color = 'first')) +
geom_density(aes(x = rnorm(100), color = 'second')) +
xlab("Age") + ylab("Density") + ggtitle('Age Densities') +
theme(legend.position = 'right') +
scale_color_manual(values = c('first' = 'red', 'second' = 'blue'))
Your data are not formatted correctly and you are basically creating two separate plots on a common "canvas", please see the code below (creation of the df is the crucial part):
library(ggplot2)
df <- data.frame(
x = c(rnorm(100), runif(100)),
col = c(rep('blue', 100), rep('red', 100))
)
ggplot(df) +
geom_density(aes(x = x, color = col)) +
xlab("Age") + ylab("Density") + ggtitle('Age Densities') +
theme(legend.position = 'right') +
scale_color_manual(labels = c('first', 'second'), values = c('red', 'blue'))
Do you how the way to combine linetype and fill in legend??
Here is my dataset:
values <- runif(1200, 1, 100)
ind <- as.factor(rep(c(1:6), each=200))
inout <- as.factor(rep(c(1:2), each =600))
df <- data.frame(values,ind,inout)
ggplot(df) +
geom_density(aes(x=values, y=..density..*100, group=interaction(ind,inout), linetype=factor(inout), colour=ind), size =1, alpha=1,na.rm = TRUE) +
geom_density(aes(x=values, y=..density..*100, group=inout, linetype=factor(inout), fill=factor(inout)), alpha=.4)
The original plot:
I would like to combine the legend "factor(inout)" and legend "NA".
Thanks for your help.
Use manual scales and make sure they both have identical names and labels (similar idea here):
ggplot(df, aes(x=values, y=..density..*100, linetype=factor(inout))) +
geom_density(aes(group=interaction(ind, inout), colour=ind),
size=1, alpha=1, na.rm=TRUE) +
geom_density(aes(group=inout, fill=factor(inout)), alpha=.4) +
scale_fill_manual(name = "fancy curves", labels = 1:2, values = c("red", "blue")) +
scale_linetype_manual(name = "fancy curves", labels = 1:2, values = 1:2)
I have a plot of multiple geom_point and a single stat_function in ggplot2. Is there a way to show a single legend?
df <- data.frame("x"=c(1:5), "a"=c(1,2,3,3,3), "b"=c(1,1.1,1.3,1.5,1.5))
df <- melt(df, "x")
p <- ggplot(df, aes(x=x, y=value)) +
geom_point(aes(colour=variable, shape=variable)) +
stat_function(aes(colour="log2(x)"), fun=log2)
I want to have a single legend with the blue line and the two colored shapes. I tried
scale_colour_discrete(name="legend", breaks=c("a", "b", "log2(x)")) +
scale_shape_discrete(name="legend", breaks=c("a", "b"))
but this does not work. Is there a way to do this automatically or by hand?
Thanks in advance.
Probably an easier alternative is to use override.aes as follows:
ggplot(df, aes(x = x, y = value)) +
geom_point(aes(colour = variable, shape = variable), size = 3) +
stat_function(aes(colour = "log2(x)"), fun = log2, size = 1.5) +
guides(shape = FALSE,
colour = guide_legend(override.aes = list(shape = c(16, 17, NA),
linetype = c("blank", "blank", "solid"))))
which results in:
Specify a . as the shape symbol for your curve and a blank line for your points:
p <- ggplot(df, aes(x=x, y=value)) +
geom_point(aes(colour=variable, shape=variable, linetype = variable), size = 3) +
stat_function(aes(colour="log2(x)", shape = "log2(x)", linetype = "log2(x)"), fun=log2) +
scale_shape_manual(values = setNames(c(16, 17, 46), c("a", "b", "log2(x)"))) +
scale_linetype_manual(values = setNames(c(0, 0, 1), c("a", "b", "log2(x)")))
print(p)