Overriding default colours in a ggplot diagram - r

I'm fairly new to R and am trying to change the colours of my generated diagram.
p = ggplot(plasma1, aes(x=Day, y=Control, colour=Supp))
+ theme(panel.background = element_rect(fill='white', colour='black'))
+ geom_point(size=2, shape=21)
+ geom_errorbar(aes(ymin=Control-SEMcontrol, ymax=Control+SEMcontrol), width=1)
p + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
+ xlab("X") + ylab("Y") + geom_line(linetype="dashed")
I've looked at themes() but I can only seem to change the grid lines and not the trend line. (Ideally I would like to change red & blue to red & black

Note: The convention on SO is that "answers" are reserved for reproducible code that demonstrates a solution. Anything less (like a suggestion) belongs in a comment. This is why it is so essential that questioners provide their data as part of the question; otherwise we have to make some up for you, which most people are (justifiably) unwilling to do.
The answer you asked for is below, but before getting into that you should be aware that ggplot's default color scheme is carefully chosen, so you should only change it if there is a good reason. The problem is that human evolution has caused certain colors (like red) to get a perceptual boost relative to other colors. So if you have a red curve and a black curve, the red curve leaves a stronger "impression". This fact is used extensively in certain fields (like advertising) to psychologically manipulate the viewer, but it has no place in scientific data visualization. The ggplot defaults, which are based on the HCL color system (which in turn is based on the Munsell color system), try to achieve two objectives: to create a color palette where each color is maximally distinguishable from all the other colors, and to even out the relative perceptual impact. There is a fairly technical discussion of this topic here, and some nice examples here.
Bottom line: don't change the colors unless you have a really good reason to do so.
Having said all that, the simple answer to your question is to use scale_color_manual(...), as below:
# all this to set up the example - you have this already
set.seed(1) # for reproducible example
x <- rep(c(1,2,4,8,11,14), each=5)
df1 <- data.frame(Day=x,Control=125*(1-exp(-x/5))+rnorm(30,sd=25),Supp="N")
df2 <- data.frame(Day=x,Control=90*(1-exp(-x/3))+rnorm(30,sd=25),Supp="C")
plasma1 <- aggregate(Control~Day+Supp,rbind(df2, df1), FUN=function(x)c(Control=mean(x),SEMcontrol=sd(x)/sqrt(length(x))))
plasma1 <- data.frame(plasma1[,1:2],plasma1[[3]])
# you start here
library(ggplot2)
ggp <- ggplot(plasma1, aes(x=Day, y=Control, color=Supp))+
geom_point(size=3, shape=21)+
geom_line(linetype="dashed")+
geom_errorbar(aes(ymax=Control+SEMcontrol, ymin=Control-SEMcontrol), width=0.3)+
theme_bw()+theme(panel.grid=element_blank())
ggp + scale_color_manual(values=c(C="red",N="black"))
Which produces this:
As mentioned in one of the comments, you could also use one of the Brewer Palettes developed by Prof. Cynthia Brewer at Penn State. These were originally intended for cartographic applications, but have become widely used generally in scientific visualization.
ggp + scale_color_brewer(palette="Set1")

Related

R - Legend For Specific Points in ggplot

Unfortunately, I think this is a tough item to reproduce, but I think the question should be simple enough to answer with a visual...
I'd like to build a legend for three specific dimensions in geom_point.
Any Hockey Fans Out There?
I'd like to build a legend for the dimensions that have colors on this chart. They are three different players I'd like to highlight, the rest of the points on the plot being general noise, but necessary for a visual.
In my opinion, here a legend would be more appealing than labels.
I know this is kind of ridiculous without being able to reproduce, but I hope the question is general enough (though I couldn't find an answer that satisfied what I was looking for) that it can be easily solved.
Happy to field questions.
Thanks!
Solved... the code looks like this:
library(ggplot2)
Offense <- read.csv("Offense1.csv")
plot <- ggplot(Offense[Offense$Gm>20,], aes(CF.Rel, SCF.Rel)) + geom_point() +
geom_point(data=Offense[Offense$Name == "Eric.Staal",], aes(colour="Eric Staal"), size=4) +
geom_point(data=Offense[Offense$Name == "Rick.Nash",], aes(colour="Rick Nash"), size=4) +
geom_point(data=Offense[Offense$Name == "Tanner.Glass",], aes(colour="Tanner Glass"), size=4)
plot <- plot + labs(title = "Driving Offense",
x = "Relative Corsi For %",
y= "Relative Scoring Chances For %")
plot <- plot + scale_colour_discrete(name="Player")
plot
They key here was to make the aesthetic color the dimension you want to include in the legend.

Removing all colors from a ggplot2 linechart

Working with RStudio 0.98.1103, I am creating two versions of exactly the same graph: One with colors and one without. Since both graphs are exactly the same (apart from the coloring) I want to avoid typing nearly the same commands again. Hence, I create the colored plot, save it, manipulate it to make it black-grey-white and save the reduced version:
library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight)) +
geom_line(aes(color=group)) + theme(legend.position="none")
bp_bw <- bp + theme_bw() +
geom_line() + theme(legend.position="none")
ggsave("bp_bw.png", bp_bw)
Although bp looks quite normal, bp_bw doesn't. There is still a blury color shining behind the black bars (red - green - blue):
Closeup:
How can I get rid of this colors, i.e. remove all color completely from bp? Only restriction: I have to create the colored graphs first (although of course a different order would work).
I think a better solution is to create a base and only add the coloring part when needed:
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight)) +
theme_bw() + theme(legend.position="none")
bp_col <- bp + geom_line(aes(color=group))
bp_bw <- bp + geom_line()
This (more-or-less) makes sense. Your bp_bw code doesn't get rid of the old colored lines, it just adds black lines on top. Anti-aliasing as the image is displayed/saved lets some of the color through on the edges.
My recommendation is to modify the color scale rather than overplot black on top:
bp_bw2 = bp + scale_color_manual(values = rep("black", 20)) + theme_bw()
This will change the colors to all black rather than plotting black on top of colors. The rep("black", 20) is kind of a hack. Apparently values aren't recycled by scale_color_manual, but extra values aren't used so you need to give it a vector at least as long as the number of colors.
This also has the advantage of not needing to repeat the geom call, and if you had previously defined a color scale this will overwrite it. If you want to be more general you could also add a scale_fill_manual(), and you probably want to specify guide = FALSE so that you don't get a very unhelpful legend.
You also might want to check out scale_colour_grey, just because it's B&W doesn't mean all the colors have to be the same.

Create a bivariate color gradient legend using lattice for an spplot overlaying polygons with alpha

I've created a map by overlaying polygons using spplot and with the alpha value of the fill set to 10/255 so that areas with more polygons overlapping have a more saturated color. The polygons are set to two different colors (blue and red) based on a binary variable in the attribute table. Thus, while the color saturation depends on the number of polygons overlapping, the color depends on the ratio of the blue and red classes of polygons.
There is, of course, no easy built-in legend for this so I need to create one from scratch. There is a nice solution to this in base graphics found here. I also came up with a not-so-good hack to do this in ggplot based on this post from kohske. A similar question was posted here and I did my best to give some solutions, but couldn't really come up with a solid answer. Now I need to do the same for myself, but I specifically would like to use R and also use grid graphics.
This is the ggplot hack I came up with
Variable_A <- 100 # max of variable
Variable_B <- 100
x <- melt(outer(1:Variable_A, 1:Variable_B)) # set up the data frame to plot from
p <- ggplot(x) + theme_classic() + scale_alpha(range=c(0,0.5), guide="none") +
geom_tile(aes(x=Var1, y=Var2, fill="Variable_A", col.regions="red", alpha=Var1)) +
geom_tile(aes(x=Var1, y=Var2, fill="Variable_B", col.regions="blue", alpha=Var2)) +
scale_x_continuous(limits = c(0, Variable_A), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, Variable_B), expand = c(0, 0)) +
xlab("Variable_A") + ylab("Variable_B") +
guides(fill=FALSE)
p
Which gives this:
This doesn't work for my purposes for two reasons. 1) Because the alpha value varies, the second color plotted (blue in this case) overwhelms the first one as the alpha values get higher. The correct legend should have blue and red mixed evenly along the 1:1 diagonal. In addition, the colors don't really properly correspond to the map colors. 2) I don't know how to overlay a ggplot object on the lattice map created with spplot. I tried to create a grob using ggplotGrob(p), but still couldn't figure out how to add the grob to the spplot map.
The ideal solution would be to create a similar figure using lattice graphics. I think that using tiles is probably the right solution, but what would be best is to have the alpha values stay constant and vary the number of tiles plotted going from left to right (for red) and bottom to top (for blue). Thus, the colors and saturation should properly match the map (I think...).
Any help is much appreciated!
How about mapping the angle to color, and alpha to the sum of the two variables -- does this do what you want?
d <- expand.grid(x=1:100, y=1:100)
ggplot(d, aes(x, y, fill=atan(y/x), alpha=x+y)) +
geom_tile() +
scale_fill_gradient(high="red", low="blue")+
theme(legend.position="none", panel.background=element_blank())

ggplot2: plotting two size aesthetics

From what I can find on stackoverflow, (such as this answer to using two scale colour gradients on one ggplot) this may not (yet) be possible with ggplot2.
I want to create a bubbleplot with two size aesthetics, one always larger than the other. The idea is to show the proportion as well as the absolute values. Now I could colour the points by the proportion but I prefer multi-bubbles. In Excel this is relatively simple. (http://i.stack.imgur.com/v5LsF.png) Is there a way to replicate this in ggplot2 (or base)?
Here's an option. Mapping size in two geom_point layers should work. It's a bit of a pain getting the sizes right for bubblecharts in ggplot though.
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(size = disp), shape = 1) +
geom_point(aes(size = hp/(2*disp))) + scale_size_continuous(range = c(15,30))
To get it looking most like your exapmle, add theme_bw():
P <- p + theme_bw()
The scale_size_continuous() is where you have to just fiddle around till you're happy - at least in my experience. If someone has a better idea there I'd love to hear it.

ggplot stat_bin2d plot with heavily skewed data

I have a set of data that is heavily right skewed. This creates a problem when doing a stat_bin2d plot. The result is most of the graph is dark blue with only a few points are a different color. I'd like to have the graph use the entire color range a bit more.
An example of the problem is from the ggplot documentation direction.
ggplot(diamonds, aes(carat, price)) + stat_bin2d()
The resulting graph has only a few positions that are something other than dark blue.
How can I adjust the mapping of the color range to show more detail? I know I can set the limits, but this doesn't exactly fit the bill as it makes anything outside the limits be gray.
ggplot(diamonds, aes(carat, price)) + stat_bin2d() + scale_fill_gradient(limits=c(1, 100))
Something like this with they gray appropriately colored too.
The quick answer is
ggplot(diamonds, aes(carat, price)) + stat_bin2d() +
scale_fill_gradient(trans="log10")
EDIT:
A longer answer is that you probably want some kind of transformation of the color or fill scale. For built-in transformations refer to the "See Also" section of
library(scales)
?trans
If none of the built-in transformation is suitable then you can construct your own. See the answers to this SO question about transforming color scales for an example showing how to do this.

Resources