how to create shifted color scale ggplot - r

I have a continous variable that I would like to map to the color of my points.
However the distribution of the variable is shifted tot the right.
So I thought I could use the quantiles to set the breaks but this doesn't seem to be working.
I think I don't understand the subtleties between all of the different variants of scale_colour_gradient.
ggplot(df, aes(x = LibPl, y = IntStd, color = totSmpInt)) +
geom_point(position = "jitter", alpha = 0.5) +
scale_colour_gradientn(colours = brewer.pal(n = 4, "RdYlBu"),
breaks = c(qn[1], qn[2], qn[3], qn[4], qn[5]))
As you can see from the color legend in the plot it doesn't really seem like the quantiles were used as break points.
I have read a few other similar posts, and tried variants, but none are working
Any help is appreciated.
Thanks,

Related

Changing color of the dots in Geom_Point

Im new to R and I have been trying to make this work but I dont seem to know how. Apologies if this is some easy fix that I'm missing.
I'm trying to make my dots in this plot a different shade of green.
Here is the code:
p = ggplot(data = swissnew, aes(x = Fertility, y = Education))
p = p + geom_point(color = "Green", size = 4, alpha = .3)
I then tried to run this
p + scale_color_manual(values = "00FF33")
It runs but no change seems to be happening to the actual plot when it shows.
This was what I ended up using.
p+geom_point(color="#00FF33",size=4,alpha=.3)
Forgetting to use the hashtag will not allow the hexadecimal code to work.
Thanks a lot guys!

Make different color (or lighten the color) for dots from smoothing line with ggplot

With ggplot, I plot a figure like this
df = data.frame(xx= seq(1,100),yy=rnorm(100)*2,
zz = rep(c("a","b"),50))
ggplot(aes(x = xx, y = yy, color = zz, group = zz), data = df) + geom_smooth() + geom_point() + theme_bw()
theme(legend.title=element_blank())
`
My original dataset has a lot of dots. Because of the same color between smoothing lines and dots in same groups, this type of figure with my data is so messy.
I would like to make pale color (or change color manually) for dots and show the lines more clearly.
How to have different colors for dots and lines with such figure?
If I'm understanding what you want correctly, you have a couple options to make the points stand out:
If the standard error shading on the lines is in your way, add se = F to geom_smooth()
Decrease the opacity of the points: geom_point(alpha = 0.5)
Change the shape of the points, for example to unfilled circles: geom_point(shape = 1)
Make the points smaller: geom_point(size = 0.5)
It's up to you which of those routes (or combination of them) you choose, but you can decide which you think is most readable for your purposes.

geom_ribbon different colours - R

I am using the following code to plot my data but I cannot manage to set the colours to geom_ribbon properly.
My graph contains 4 lines, each of one with a different color. I want the 'geom_ribbon' of each line to have the same color as its line (with transparency - alpha).
In addition, when I change the value of alpha (e.g. from 0.1 to 0.9) I dont't see any change on the transparency. Finally, an extra class is added in the legend and I would like to remove this? Any help on this basic ggplot?
ggplot(dfmean_forplot, aes(x = image, y = value, group = ID)) +
geom_line(aes(colour=factor(ID)))+
scale_x_discrete(breaks=1:21,
labels=c("19/1","7/2","17/2","18/3","17/4","27/4","17/5","27/5","7/6","16/6","26/6","5/7","16/7","6/8","15/8","25/8","4/9","25/9","4/10","14/10","22/11"))+
xlab("# reference")+
ylab("value")+
scale_colour_discrete(name = "class")+
ylim(0,0.9)+
geom_ribbon(aes(ymin=dfmean_forplot$value-dfsd_forplot$value, ymax=dfmean_forplot$value+dfsd_forplot$value, alpha = 0.3))
EDIT
What about the legend? Ideally, I would like to combine them so that there is a square for each color crossed by a line of the same color
You need to add the fill aesthetic and take alpha outside aes, both for geom_ribbon. The following code should solve that.
ggplot(dfmean_forplot, aes(x = image, y = value, group = ID)) +
geom_line(aes(colour=factor(ID)))+
scale_x_discrete(breaks=1:21,
labels=c("19/1","7/2","17/2","18/3","17/4","27/4","17/5","27/5","7/6","16/6","26/6","5/7","16/7","6/8","15/8","25/8","4/9","25/9","4/10","14/10","22/11"))+
xlab("# reference")+
ylab("value")+
scale_colour_discrete(name = "class")+
ylim(0,0.9)+
geom_ribbon(aes(ymin=dfmean_forplot$value-dfsd_forplot$value,
ymax=dfmean_forplot$value+dfsd_forplot$value,
fill = factor(ID)), alpha = 0.3)

Conditional graphing and fading colors

I am trying to create a graph where because there are so many points on the graph, at the edges of the green it starts to fade to black while the center stays green. The code I am currently using to create this graph is:
plot(snb$px,snb$pz,col=snb$event_type,xlim=c(-2,2),ylim=c(1,6))
I looked into contour plotting but that did not work for this. The coloring variable is a factor variable.
Thanks!
This is a great problem for ggplot2.
First, read the data in:
snb <- read.csv('MLB.csv')
With your data frame you could try plotting points that are partly transparent, and setting them to be colored according to the factor event_type:
require(ggplot2)
p1 <- ggplot(data = snb, aes(x = px, y = py, color = event_type)) +
geom_point(alpha = 0.5)
print(p1)
and then you get this:
Or, you might want to think about plotting this as a heatmap using geom_bin2d(), and plotting facets (subplots) for each different event_type, like this:
p2 <- ggplot(data = snb, aes(x = px, y = py)) +
geom_bin2d(binwidth = c(0.25, 0.25)) +
facet_wrap(~ event_type)
print(p2)
which makes a plot for each level of the factor, where the color will be the number of data points in each bins that are 0.25 on each side. But, if you have more than about 5 or 6 levels, this might look pretty bad. From the small data sample you supplied, I got this
If the levels of the factors don't matter, there are some nice examples here of plots with too many points. You could also try looking at some of the examples on the ggplot website or the R cookbook.
Transparency could help, which is easily achieved, as #BenBolker points out, with adjustcolor:
colvect = adjustcolor(c("black", "green"), alpha = 0.2)
plot(snb$px, snb$pz,
col = colvec[snb$event_type],
xlim = c(-2,2),
ylim = c(1,6))
It's built in to ggplot:
require(ggplot2)
p <- ggplot(data = snb, aes(x = px, y = pz, color = event_type)) +
geom_point(alpha = 0.2)
print(p)

Adding points to GGPLOT2 Histogram

I'm trying to produce a histogram that illustrates observed points(a sub-set) on a histogram of all observations. To make it meaningful, I need to color each point differently and place a legend on the plot. My problem is, I can't seem to get a scale to show up on the plot. Below is an example of what I've tried.
subset <-1:8
results = data.frame(x_data = rnorm(5000),TestID=1:5000)
m <- ggplot(results,aes(x=x_data))
m+stat_bin(aes(y=..density..))+
stat_density(colour="blue", fill=NA)+
geom_point(data = results[results$TestID %in% subset,],
aes(x = x_data, y = 0),
colour = as.factor(results$TestID[results$TestID %in% subset]),
size = 5)+
scale_colour_brewer(type="seq", palette=3)
Ideally, I'd like the points to be positioned on the density line(but I'm really unsure of how to make that work, so I'll settle to position them at y = 0). What I need most urgently is a legend which indicates the TestID that corresponds to each of the points in subset.
Thanks a lot to anyone who can help.
This addresses your second point - if you want a legend, you need to include that variable as an aesthetic and map it to a variable (colour in this case). So all you really need to do is move colour = as.factor(results$TestID[results$TestID %in% subset]) inside the call to aes() like so:
ggplot(results,aes(x=x_data)) +
stat_bin(aes(y=..density..))+
stat_density(colour="blue", fill=NA)+
geom_point(data = results[results$TestID %in% subset,],
aes(x = x_data,
y = 0,
colour = as.factor(results$TestID[results$TestID %in% subset])
),
size = 5) +
scale_colour_brewer("Fancy title", type="seq", palette=3)

Resources