无法设置瓷砖图中颜色的数值范围。

huangapple go评论78阅读模式
英文:

Can´t set the numerical range for the colors in tile plot

问题

我正在使用此代码创建一个瓷砖图,其中每种颜色应通过颜色代码显示给定的数值数据范围。问题是,我的数据具有广泛的数值范围,而且大多数数字都很低。我不想使用数据转换,而是尝试在代码中设置颜色应该使用的间隔。在"values"中,我试图设置每种颜色的限制,但最终的图表不显示我需要的内容。

mean_data <- aggregate(variacao_proporcional ~ causa_variacao_turb + tipo_ambiente, data, mean)

num_breaks <- 9

breaks <- quantile(data$variacao_proporcional, probs = seq(0, 1, length.out = num_breaks))

breaks_mean <- quantile(mean_data$variacao_proporcional, probs = seq(0, 1, length.out = num_breaks))

values <- c(0.02, 0.8, 1.2, 1.8, 2.5, 3.5, 5.9, 26, 402)

colors <- colorRampPalette(c("green", "lightgreen", "gray", "yellow", "gold", "orange", "darkorange", "red", "darkred"))(length(values) - 1)

ggplot(mean_data, aes(x = tipo_ambiente, y = causa_variacao_turb, fill = variacao_proporcional)) +
  geom_tile() +
  scale_fill_gradientn(colors = colors, breaks = breaks_mean, values = values) +
  theme_classic() 

正如您从我的数据分布中可以看到的那样,我有非常低的数值。我创建了一个渐变,以绿色显示较低的值,而所有大于0.8的值应显示为灰色,而大于1的值应从黄色到深绿色显示。

对于如何制作这个图,有什么想法吗?

英文:

I am using this code to create a tile plot in which each color should show a given numerical data range through a color code. The problem is that my data have a wide numerical range and most of numbers are low. I dont´want to use data transformation, instead I´m trying to set in the code the intervals in which the colors should be used. In values I tried to set the limits to each color but the final plot is not showing what I need.

mean_data &lt;- aggregate(variacao_proporcional ~ causa_variacao_turb + tipo_ambiente, data, mean)

num_breaks &lt;- 9

breaks &lt;- quantile(data$variacao_proporcional, probs = seq(0, 1, length.out = num_breaks ))

breaks_mean &lt;- quantile(mean_data$variacao_proporcional, probs = seq(0, 1, length.out = num_breaks))

print(breaks_mean)
      0%      12.5%        25%      37.5%        50%      62.5%        75%      87.5% 
0.026250   0.795000   1.252750   1.873750   2.473750   3.348333   5.824375  25.951250 
    100% 
401.860000


values &lt;- c(0.02, 0.8, 1.2, 1.8, 2.5, 3.5, 5.9, 26, 402)

colors &lt;- colorRampPalette(c(&quot;green&quot;, &quot;lightgreen&quot;, &quot;gray&quot;, &quot;yellow&quot;, &quot;gold&quot;, &quot;orange&quot;, &quot;darkorange&quot;, &quot;red&quot;, &quot;darkred&quot;))(length(values) - 1)

ggplot(mean_data, aes(x = tipo_ambiente, y = causa_variacao_turb, fill = variacao_proporcional)) +
geom_tile() +
scale_fill_gradientn(colors = colors, breaks = breaks_mean, values = values) +
theme_classic() 

As you can see from my data distribution, i have very low values. I created a gradient that shows the lower values in green colors and all values above 0.8 should be shown in gray while values higher than 1 should appear from yellow to dark green.

Any ideas on how can I make this plot?

无法设置瓷砖图中颜色的数值范围。

答案1

得分: 1

我认为您想要从连续变量variacao_proporcional中创建类别以使其离散化,并且您希望您的离散填充覆盖您指定的一系列颜色。以下是一个使用ggplot2包的示例。我使用了cut命令来创建一个有序(离散)因子price_cat

values <- c(0, 300, 500, 800, 1000, 1500, 3000, 12000, 20000)
colors <- colorRampPalette(c("green", "lightgreen", "gray", "yellow", "gold", "orange", "darkorange", "red", "darkred"))(length(values) - 1)
diamonds2 <- diamonds
diamonds2$price_cat = cut(diamonds2$price, breaks = values) 

ggplot(diamonds2, aes(x = cut, y = carat, fill = price_cat)) +
  geom_jitter(shape = 21, color = "white", stroke = 0.1) +
  scale_fill_manual(values = colors) +
  theme_classic() 

如果您想要像您的代码和图片中一样使用连续刻度,转换可能会有所帮助。您提到您不想使用一个,但在我看来,在这里使用一个是有用的 - 请注意,我们可以将其应用于映射,而不必手动更改任何基础值。请注意,颜色图例仍然涉及到基础值(而不是其转换),但映射本身已经被转换为均匀分割数据范围,为500-2000和5000-15000提供了大致相同的颜色空间。

ggplot(diamonds, aes(x = cut, y = carat, fill = price)) +
  geom_jitter(shape = 21, color = "white", stroke = 0.1) +
  scale_fill_gradientn(colors = colors, 
                       breaks = c(200, 500, 1000, 2000, 5000, 15000),
                       trans = scales::pseudo_log_trans(sigma = 100)) +
  theme_classic() 
英文:

I think you want to make categories from your continuous variacao_proporcional variable to make it discrete, and you want your discrete fill to span a range of colors you specify. Here's an example with data from the ggplot2 package. I used the cut command to make an ordered (discrete) factor price_cat.

values &lt;- c(0, 300, 500, 800, 1000, 1500, 3000, 12000, 20000)
colors &lt;- colorRampPalette(c(&quot;green&quot;, &quot;lightgreen&quot;, &quot;gray&quot;, &quot;yellow&quot;, &quot;gold&quot;, &quot;orange&quot;, &quot;darkorange&quot;, &quot;red&quot;, &quot;darkred&quot;))(length(values) - 1)
diamonds2 &lt;- diamonds
diamonds2$price_cat = cut(diamonds2$price, breaks = values) 


ggplot(diamonds2, aes(x = cut, y = carat, fill = price_cat)) +
  geom_jitter(shape = 21, color = &quot;white&quot;, stroke = 0.1) +
  scale_fill_manual(values = colors) +
  theme_classic() 

无法设置瓷砖图中颜色的数值范围。

If you want to use a continuous scale like in your code and picture, a transformation would be helpful. You note you don't want to use one, but it seems useful here to me -- note that we can apply it to the mapping without manually altering any of the underlying values. Note that the color legend still refers to the underlying values (not transformations of them), but the mapping itself is transformed to segment the data range more evenly, giving about the same amount of color space to 500-2000 as 5000-15000.

ggplot(diamonds, aes(x = cut, y = carat, fill = price)) +
  geom_jitter(shape = 21, color = &quot;white&quot;, stroke = 0.1) +
  scale_fill_gradientn(colors = colors, 
                       breaks = c(200, 500, 1000, 2000, 5000, 15000),
                       trans = scales::pseudo_log_trans(sigma = 100)) +
  theme_classic() 

无法设置瓷砖图中颜色的数值范围。

huangapple
  • 本文由 发表于 2023年6月22日 05:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定