R ggplot Legend shows shapes inccorectly - r

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)

Related

Fill region below stairs plot with a particular transparency using Makie.jl in Julia

I'd like to fill the region below a stairs!() plot (between the stairs and the x axis) in order to get something like the gray area in the image, but with a specific % transparency.
A somewhat hacky way (digs into the stair plot to get the graphed points):
function stairpts(s)
pts = s.plots[1].converted[1][]
[p[1] for p in pts], [p[2] for p in pts]
end
s = stairs!(xs, ys, step=:post)
xs′, ys′ = stairpts(s)
band!(xs′, 0*ys′, ys′, color=(s.color, 0.25)) # 0.25 alpha
Creating a recipe for this
type of plot is probably better.
What you show looks like multiple barplots or histograms (hist!() function or barplot!() function) with transparency allowing see-through overlap. Could you do this using barplot or hist! (depending on your data) instead of stairs!? If you want the barplots to be more uniform in outline, you could make the outline strokewidth thicker and the same strokecolor as the bars.

Strange line connection using ggplot geom_line colours

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.

ggplot2/ggmap: Use two-color point markers in both plot and legend

I'm having a dataframe samp, with userid, latitude, longitude, mb. I wanted to plot a map with the points proportional to MB used. I wanted a donut kind of shape in geom_point, so I thought I can use two pch = 20 with varying sizes to get the donut shape of pch. But I am facing some problems in it.
m <- get_map(location=c(lon=median(samp$longitude),lat=median(samp$latitude) ), zoom=10)
print(ggmap(m) +
geom_point(aes(x=longitude, y=latitude, size= mb.user), colour="orange", pch = 20, data=samp) +
geom_point(aes(x=longitude, y=latitude, size= mb.user), colour="black", pch = 20, size = 4, data=samp))
but I am getting something like,
The shapes are not even throughout the map. I want the shapes to be even and proportional to mb.user values. But the map here is neither proportional to mb.user or the sizes.
Also the legend is also showing only one color. It isn't showing two colours together. I ideally want to have a donut shaped symbol whose size is proportional to mb.user.
Can anybody help me in finding the mistake I am doing here?
Thanks
If you use a point shape that has a border, you can plot the points just once and it will show up properly in the legend. If you have ggplot2 version 2 installed (latest version is 2.1.0 as of this writing), you can also control the width of the point border using the stroke parameter. You haven't provided a reproducible example, so here's an example using the built-in mtcars data frame:
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(size=mpg), colour="red", fill="black", shape=21, stroke=1.5) +
scale_size_area(max_size=4)
shape=21 is a filled circle with a border (see ?pch for available shapes). colour sets the border color, fill sets the fill color, and stroke sets the border width.
Regarding your original code, the black circles are all the same size, because you've overridden size=mb.user by also setting size=4 outside the call to aes. You can't see some of the orange points in cases where the black points are larger than the orange points. If you remove size=4 and do size=0.3*mb.user inside aes, you'll get properly scaled black points inside the scaled orange points.
However, that still won't solve the legend issue. I don't think there's a way to get a legend with black-inside-orange points using two separate calls to geom_point since there's no way (at least none that I can think of) to create a combined size/color mapping to do that. Using a single call geom_point with a filled marker solves the problem, but I thought I'd try to explain as best I could why your original code wasn't working as you expected.

Change contour colours using directlabels

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"))

Unusual legend using size mapping and density2d

I am trying to make a scatterplot in ggplot2 with a size mapping to a third variable and density2d contours. It seems as if the legend is being confused by the inclusion of density2d contours.
For example, the following code works:
library('ggplot2')
set.seed(1)
x=rnorm(100); y=rnorm(100,sd=10); z=seq(1,10,length.out=100)
dd=data.frame(x=x,y=y,z=z)
ggplot(dd,aes(x,y,size=z))+geom_point()
But now, note the legend behaves unusually when I add in a call to stat_density2d(). In particular, the plot legend shows blue blocks instead of black circles:
ggplot(dd,aes(x,y,size=z))+geom_point()+stat_density2d()
As size= is one of the aesthetics you can set for the stat_density2d() and in this case it is set in ggplot() call, legend is made for both - lines and points (points are hided under lines in legend as geom_point() is called before stat_density2d()). To remove blue lines from legend, you can set manually size=0.5 (or some other value) inside the stat_density2d() and then legend will be correct.
ggplot(dd,aes(x,y,size=z))+geom_point()+stat_density2d(size=0.5)

Resources