Remove point transparency in ggplot2 legend [duplicate] - r

This question already has answers here:
ggplot legend showing transparency and fill color
(1 answer)
How to set legend alpha with ggplot2
(2 answers)
Closed 9 years ago.
In ggplot2, transparency that is defined in geom_XXX is reflected in the legend. For example:
df <- data.frame(x=runif(10000), z=ifelse(runif(10000) > 0.5, 'a', 'b')); df$y <- runif(10000); df$y[df$z == 'b'] <- cos(df$x[df$z == 'b']*10)
ggplot(df) + geom_point(aes(x, y, color=z), alpha=0.1)
Gives the following result:
Since the points are very transparent, they are hardly seen on the legend. I would like to remove point transparency from the legend, so that the graph looks like this:
How is this possible?

You can use function guides() and override.aes= to set alpha value just for legend entries.
ggplot(df) + geom_point(aes(x, y, color=z), alpha=0.1)+
guides(colour = guide_legend(override.aes = list(alpha=1)))

Related

ggplot2 geom_line colors by group but with different colorcodes [duplicate]

This question already has answers here:
How to set multiple legends / scales for the same aesthetic in ggplot2?
(2 answers)
Manually setting group colors for ggplot2
(1 answer)
Changing line colors with ggplot()
(2 answers)
Closed 10 months ago.
does anyone know how I can plot several line graphs in one graph with different color codes?
ggplot(df, aes(x=variable1)) +
geom_line(aes(y=variable2,color=group1))+
geom_line(aes(y=variable3,color=group1))
I would like to have one color code for the first geom_line and a different one for the second geom_line.
color_group <- c("blue","black","yellow2","orange")
color_flag <- c("green","red","yellow2","cyan")
With
scale_colour_manual(values=color_group)
I can only assign a color code to both of them simultaneously and not separately. Thanks for your help!
You could use the ggnewscale package
library(ggnewscale)
ggplot(df, aes(x = variable1)) +
geom_line(aes(y = variable2, color = group1)) +
scale_colour_manual(values = color_group) +
new_scale_color() +
geom_line(aes(y = variable3, color = group1)) +
scale_colour_manual(values = color_flag)

How to change default color of points in ggplot2 conditional aes? [duplicate]

This question already has answers here:
how to change the color in geom_point or lines in ggplot [duplicate]
(2 answers)
Closed 4 years ago.
I want to change the default colors (currently blue and red) of the points in ggplot2 to another color set.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color=displ<5))
ggplot(data=mpg) +
geom_point(mapping = aes(x=displ, y=hwy, color=displ<5)) +
scale_colour_manual(values=c("gold", "red"))
Here, we use a logical vector, which is converted to numeric to provide a two-color scale i.e. the value for aes(color) is as.integer(mpg$displ < 5).
We then convert this into another two-color scale of our choice, here using named values for colors. This gives:
For more choices for colors, see the following (in package:grDevices, so should be loaded by default):
demo("colors")

How to give color to specific points in violin plot in R [duplicate]

This question already has an answer here:
ggplot2: How to specify multiple fill colors for points that are connected by lines of different colors
(1 answer)
Closed 4 years ago.
I created a violin plot in R, and I want to change part of the dots color and size. This change will be according to an attribute of True/False in the data file.
This is how I tried it:
p <- ggplot(data, aes(order,count),levels=data_levels) +
geom_point(aes(colour = actor(data$color)))+
geom_violin(draw_quantiles = c(0.5),adjust = 2,size =0.4)+
geom_jitter(height = 0, width = 0.1,size =0.6)+
coord_flip()
boolColors <- as.character(c("False"="black", "True"="red"))
boolScale <- scale_colour_manual(name="color", values=boolColors)
p1 <- p + boolScale
I tried changing the size with scale_size_manual but it didn't work.
Without the data, it is had to know exactly what is wrong. But here is an example of setting a custom colour scale for points over a violin plot.
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_violin() +
geom_jitter(height = 0, width = 0.1, aes(colour = factor(gear))) +
scale_colour_manual(name="colour", values=c("pink", "purple", "orange"))

Add line to Boxplot in GGPLOT2 [duplicate]

This question already has an answer here:
How to background geom_vline and geom_hline in ggplot 2 in a bubble chart
(1 answer)
Closed 6 years ago.
Is there a way to add a horizontal line to a boxplot in ggplot2, that doesn't cut through the existing plot, but only the spaces in between?
Thanks for your help...
ggplot adds up each geom one after another, so...
library(ggplot2)
df <- data.frame(x = gl(3,1), y = 1:3)
ggplot(df, aes(x,y)) +
geom_hline(yintercept = 1.5) +
geom_col(width = .5)
... places a horizontal line under the bars produced by geom_col.

using two scale colour gradients on one ggplot [duplicate]

This question already has answers here:
How to set multiple legends / scales for the same aesthetic in ggplot2?
(2 answers)
Closed last year.
I would like to combine a colour scale gradient for the points on a scatter plot together with a colour scale gradient for some text that goes on the plot. I can do them separately as shown in my example below, but i can't seem to put them together...is there a way of doing this?
Here is my example code of the two types of plots (p and p1) that I want to combine
l <- data.frame(prev=rnorm(1266),
aft=rnorm(1266),
day=as.factor(wday(sample(c(2:6),1266,replace=TRUE),abbr=TRUE, label=TRUE)),
month=as.factor(month(Sys.Date()+months(sample(0:11,1266,replace=TRUE)),abbr=TRUE, label=TRUE)),
ind=c(1:1266))
cors <- ddply(l, c("month", "day"), summarise, cor = round(cor(prev, aft), 3))
# below the text gains the colour gradient
p <- ggplot(l, aes(x=prev, y=aft)) +
geom_point() +
scale_colour_gradient(low = "red", high="blue")+
facet_grid(day~month, scales="free_x")+
geom_text(data=cors,aes(label=paste("r= ",cor,sep=""), size=abs(cor), colour=cor), x=Inf, y=Inf, vjust=1, hjust=1, show_guide=FALSE)+
geom_hline(aes(yintercept=0))+
geom_smooth(method="loess")
p
# below the points gain the colour gradient
p1 <- ggplot(l, aes(x=prev, y=aft)) +
geom_point(aes(colour=ind)) +
scale_colour_gradient("gray")+
facet_grid(day~month, scales="free_x")+
geom_text(data=cors,aes(label=paste("r= ",cor,sep=""), size=abs(cor), colour=cor), x=Inf, y=Inf, vjust=1, hjust=1, show_guide=FALSE)+
geom_hline(aes(yintercept=0))+
opts(legend.position="none") +
geom_smooth(method="loess")
p1
I do not expect that this can be done. A plot only has one scale per aesthetic. I believe that if you add multiple scale_color's, the second will overwrite the first. I think Hadley created this behavior on purpose, within a plot the mapping from data to a scale in the plot, e.g. color, is unique. This ensures that all color in the plot can be compared easily, because they share the same scale_color.

Resources