I was wondering how I could change the fill color of the square next to beta in my legend below to be according to: adjustcolor("blue",alpha.f=0.05)?
Here is my current R code (with no success):
curve(dt(x,19),-5,5)
legend("topright",legend=c(expression(paste("Power")),expression(paste(beta))),
density=c(15,0),angle=140,fill=T,col=c(adjustcolor("blue",alpha.f=0.05),0))
Something like this should work:
legend("topright",legend=c(expression(paste("Power")),expression(paste(beta))),
density=c(15,0),angle=140,fill=T,border=c('black', 'blue'))
[EDITED]
legend("topright",legend=c(expression(paste("Power")),expression(paste(beta))),
density=c(15,NA),angle=140,fill=c("#000000FF", adjustcolor("blue",alpha.f=0.5)),
border=c("#000000FF", adjustcolor("blue",alpha.f=0.5)))
Related
A problem occurs when i add a colour(gradient) into the aesthetics of geom_line. It seems that the dots are no longer connected by a line but instead transformed into squares, which messes up the plot. Does anyone have an idea how to solve this problem or if this is how R or ggplot handles colour(gradients). This problem doesn't occur, when i remove the colours from the aes().
Thanks in advance!
Simple code:
df <- data.frame(c(1,3,4,2,5,8),c(1,2,3,4,5,6))
colnames(df) <- c("x","y")
ggplot()+
theme_classic()+
geom_line(data=df,aes(x=x,y=y,colour=y),size=2)
ggplot()+
theme_classic()+
geom_line(data=df,aes(x=x,y=y),size=2)
Adjusting the lineend parameter should do it, which has a default of "square". The reason the uncolored version looks okay is that in those cases the segments are treated as continuations of the same series, so they are governed then by the linejoin parameter, which has a default of "round."
ggplot()+
theme_classic()+
geom_line(data=df,aes(x=x,y=y,colour=y),size=2, lineend = "round")
PS - if you want the color to vary within each segment, you might look at alternatives like ggforce::geom_link2, which uses interpolation to allow smooth transitions. However, it's parameterized differently, to be like geom_path, which plots data in order of appearance rather than in order of the x value, like geom_line.
Using Windows Server 2008 R2 Standard; R 3.5.3; Rstudio 1.1.453; ggplot2 3.2.1
When I create a plot using ggplot2 that includes a continuous variable that is displayed using color, the legend appears with the range of reference values, but the color scale to the left of the numbers in the legend does not appear (it just remains white to the left of the numbers). How do I make the continuous color scale appear in the legend? Example below and I've attached the resulting plot. The color scale is missing in Rstudio and also when I export to png using png() function.
ggplot(mtcars,
aes(x = qsec,
y = mpg,
colour = wt)) +
geom_point()
image of resulting plot, which is missing the color scale in the legend
mlcyo's suggestion (and code in link) fixed the problem when the plot is saved to png. Thank you!
Is there a reason why ggplot might mess with the geom_point shape in the legend?
In the actual plot everything looks with the shapes correctly plotted as circles, but in the legend it shows them as weird boxes / squares, i.e. it is showing this:
But it should show this:
Could it be because I have an ifelse in my geom_point ? This is what I have here for this part:
geom_point(aes( y=y, colour=ifelse( (ty>308)&(Time < chron(times=c('08:30:30.0'))), ifelse(side=='left', 'red', 'blue'),'gray')), na.rm = T)
This issue is actually because geom_point and geom_line are both plotted and the points are varying according to the size parameter. ggplot is trying to show a point on a line which looks good when it is small and clear but becomes strange and box-like as the size varies.
To make it clearer, turning off the legend for either the line or the points will keep just one.
For example:
geom_line(aes(y=foo , colour='green'), show.legend = F)
I'm fairly new to ggplot2, and I'm trying to create a contour plot of data that has missing values. Because there's missing values I can't have the contours by themselves, so I'm combining a tiles background with a contour. The problem is the labels are the same colour as the background.
Suppose I have data like so:
DF1 <- data.frame(x=rep(1:3,3),y=rep(1:3,each=3),z=c(1,2,3,2,3,4,3,NA,NA))
I can make a plot like this:
require(ggplot2); require(directlabels)
plotDF <- ggplot(DF1,aes(x,y,z=z)) + geom_tile(aes(fill=z)) + stat_contour(aes(x,y,z=z,colour= ..level..),colour="white")
direct.label(plotDF)
This gives me a plot similar to what I want but I'd like to be able to change the colours of the labels to be black. Any ideas?
I spotted a similar post and thought this would be easy, something along the lines of direct.label(p, list("last.points", colour = "black"). I could not make it work, unfortunately; I believe, this is not directly supproted.
I then decided to use black magic and managed to do the trick by manually overriding the colour scale:
direct.label(plotDF +
scale_colour_gradient(low="black", high="black"))
The following code assigns a manual color scale of red and black to my points:
require(ggplot2)
require(directlabels)
dtest <- data.frame(x=1:20,
y=rnorm(20,0,5),
v=seq(1,2))
p <- ggplot(dtest, aes(x=x,y=y,color=as.factor(v))) + geom_point() + scale_colour_manual(values=c("red","black"))
p #this looks good; red and black as intended
direct.label(p) #this falls back on the default colors
But when I apply direct.label() to the same plot, it overrides the color scale in favor of the ggplot default. Is there a way to prevent this? If not, what's the best way to assign new colors to the default ggplot scale?
Thanks,
Matt
This happens because direct.label(p) operates by adding the label geom to p, then by hiding the color legend, since labeling the colors twice would be redundant. One way to hide the color legend is by adding scale_colour_discrete(legend=FALSE), and this is what I do inside of direct.label. So when directlabels applies scale_colour_discrete, your scale_colour_manual will be lost. The workaround is to use the following idiom:
p <- ggplot(...)
direct.label(p)+
scale_colour_manual(...)