Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to apply color gradient on histogram in ggplot

I am floundering with colors in ggplot. I am trying to apply a color gradient based on the rank column below. I am pretty sure this is a discrepancy between color and fill or discrete and continuous variables. I want colors as shown on the scale in "c" and "d" below, but my closest attempts are "e" and "f" where the points are colored but not colored by gradient. The gradient I prefer applies to values of rank 1:100, with all others values' points black.

Any assistance would be most appreciated.

library(reshape2)
library(ggplot2)

co2 <- read.table(
  header=TRUE, text='
rank tons
1     2 1.00
2     4 1.00
3     7 0.00
4    44 0.00
5   104 0.00
6    48 0.05
7    32 0.50
8     5 0.00
9    78 1.00
10   12 0.00
11   15 0.00
12  176 1.00
13  440 0.02
14  249 0.00
15  481 0.00
16  388 0.00
17  458 0.05
18  488 0.00
19  264 0.00
20  203 0.00
            ')      

I tried:

#does not add rank as a color
c<- ggplot(data=co2, aes(x = tons, color=rank)) 
c + geom_dotplot(stackgroups = TRUE, binwidth = .05, binpositions = "all")  +
    scale_colour_gradient(limits=c(1, 500))

#also does not add rank as color
d<- ggplot(data=co2, aes(x = tons, color=rank)) 
d + geom_dotplot(stackgroups = TRUE, binwidth = 0.05, method = "histodot")  + 
    scale_colour_gradient(limits=c(1, 100))

#create breaks for fill-- works correctly but no gradient
co2$brks<- cut(co2$rank, c(seq(0, 100, 20), max(co2$rank)))
e<- ggplot(data=co2, aes(x = tons, fill=brks)) 
e + geom_dotplot(stackgroups = TRUE, binwidth = 0.05, method = "histodot")  

#also works correctly but no gradient
f<- ggplot(data=co2, aes(x = tons, fill=brks)) + geom_histogram() 
f 

I checked these already but I'm still missing something:

  • Color Gradients With ggplot
  • Use histogram breaks to apply function over second column
  • gradient breaks in a ggplot stat_bin2d plot
  • http://docs.ggplot2.org/current/geom_dotplot.html
like image 398
Jack Ryan Avatar asked Mar 04 '13 13:03

Jack Ryan


People also ask

How do you add color to a histogram in R?

If you want to change the colors of the default histogram, you merely add the arguments border or col . You can adjust, as the names itself kind of give away, the borders or the colors of your histogram.

How do you change the gradient color in R?

Key functions to change gradient colors The default gradient colors can be modified using the following ggplot2 functions: scale_color_gradient() , scale_fill_gradient() for sequential gradients between two colors. scale_color_gradient2() , scale_fill_gradient2() for diverging gradients.

Can you build a histogram using ggplot2?

You can also make histograms by using ggplot2 , “a plotting system for R, based on the grammar of graphics” that was created by Hadley Wickham. This post will focus on making a Histogram With ggplot2.

Which method is used to create a histogram using ggplot2?

This R tutorial describes how to create a histogram plot using R software and ggplot2 package. The function geom_histogram() is used. You can also add a line for the mean using the function geom_vline.


2 Answers

This is a bit of a hacky answer, but it works:

##Define breaks
co2$brks<- cut(co2$rank, c(seq(0, 100, 5), max(co2$rank)))
#Create a plot object:
g = ggplot(data=co2, aes(x = tons, fill=brks)) +
   geom_dotplot(stackgroups = TRUE, binwidth = 0.05, method = "histodot")

Now we manually specify the colours to use as a palette:

 g + scale_fill_manual(values=colorRampPalette(c("white", "red"))( length(co2$brks) ))

enter image description here

like image 194
csgillespie Avatar answered Oct 01 '22 20:10

csgillespie


I just needed to add

+ scale_fill_brewer(palette="RdYlBu")

See below:

library(reshape2)
library(ggplot2)

co2 <- read.table(
  header=TRUE, text='
rank tons
1     2 1.00
2     4 1.00
3     7 0.00
4    44 0.00
5   104 0.00
6    48 0.05
7    32 0.50
8     5 0.00
9    78 1.00
10   12 0.00
11   15 0.00
12  176 1.00
13  440 0.02
14  249 0.00
15  481 0.00
16  388 0.00
17  458 0.05
18  488 0.00
19  264 0.00
20  203 0.00
            ')                                                                                                          

#create breaks for fill--
co2$brks<- cut(co2$rank, c(seq(0, 100, 20), max(co2$rank)))
e<- ggplot(data=co2, aes(x = tons, fill=brks)) 
e + geom_dotplot(stackgroups = TRUE, binwidth = 0.05, method = "histodot")  + scale_fill_brewer(palette="RdYlBu")

#also works correctly!
ggplot(data=co2, aes(x = tons, fill=brks)) + geom_histogram() + scale_fill_brewer(palette="RdYlBu")
like image 39
Jack Ryan Avatar answered Oct 01 '22 20:10

Jack Ryan