尝试在ggplot中的直方图上应用颜色渐变

时间:2022-11-30 21:17:56

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.

我在ggplot中挣扎着颜色。我正在尝试根据下面的排名列应用颜色渐变。我很确定这是颜色和填充或离散和连续变量之间的差异。我希望颜色如下面“c”和“d”中的刻度所示,但我最接近的尝试是“e”和“f”,其中点着色但不是渐变着色。我更喜欢的渐变适用于等级1的值:100,其他所有值的值都是黑色。

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:

我已经检查了这些,但我仍然遗漏了一些东西:

2 个解决方案

#1


4  

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

这是一个hacky答案,但它的工作原理:

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

尝试在ggplot中的直方图上应用颜色渐变

#2


1  

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

#1


4  

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

这是一个hacky答案,但它的工作原理:

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

尝试在ggplot中的直方图上应用颜色渐变

#2


1  

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