英文:
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 <- 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))
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 <- 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()
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 <- 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()
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 = "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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论