R用于打印的自定义颜色比例
我正在尝试创建一个自定义比例,以更好地显示我拥有的数据。对于第一个图,数据相对一致,介于0和3之间。然而,对于我的第二个数据集,大多数数据都少于100,大约90%的数据都在1000以下。(下图) 我试图绘制的包含值的列名为“data1”,值是0-10000之间的整数 我试着通过插入R用于打印的自定义颜色比例,r,ggplot2,maps,R,Ggplot2,Maps,我正在尝试创建一个自定义比例,以更好地显示我拥有的数据。对于第一个图,数据相对一致,介于0和3之间。然而,对于我的第二个数据集,大多数数据都少于100,大约90%的数据都在1000以下。(下图) 我试图绘制的包含值的列名为“data1”,值是0-10000之间的整数 我试着通过插入 map.df$brks <- cut(map.df$data1, breaks=c(0, 1, 10, 25, 100, 250, 1000, 10000),
map.df$brks <- cut(map.df$data1,
breaks=c(0, 1, 10, 25, 100, 250, 1000, 10000),
labels=c("0", "1 - 10", "11 - 25", "26 - 100",
"101 - 250", "251 - 1000", "1001 - 10000"))
map.df$brks你就快到了。注释代码中的解释如下:
# add include.lowest = TRUE, otherwise the 0 values will become NAs
map.df$brks <- cut(map.df$data1,
breaks = c(0, 1, 10, 25, 100, 250, 1000, 10000),
labels = c("0", "1 - 10", "11 - 25", "26 - 100",
"101 - 250", "251 - 1000", "1001 - 10000"),
include.lowest = TRUE)
# use scale_fill_brewer rather than scale_fill_gradientn; it expects
# a categorical variable (e.g. brks) while scale_fill_gradientn expects
# a continuous variable (e.g. data1).
# also, I suggest placing the legend at the side, as a vertical stack of
# boxes, which resembles a color bar.
# theme_void can be used to remove all backgrounds, axis labels, etc.
ggplot(map.df, aes(x=long, y=lat, group=group, fill=brks)) +
geom_polygon(colour = "grey", size = 0.001) +
coord_map() +
expand_limits(x = map.county$long, y = map.county$lat) +
scale_fill_brewer(palette = "YlOrRd") +
theme_void() +
theme(legend.position = "right")
#add include.lowest=TRUE,否则0值将变为NAs
您能否简化您的问题,并给出一个我们可以使用的示例数据集,并澄清“自定义颜色比例”的含义。它给你的那个有什么问题吗?我很抱歉不清楚。当我说“自定义颜色比例”时,我的意思是能够改变比例,使颜色不会以恒定速率变化。如上图所示,每2000个单位都有等距的记号。我希望能够将这些数字替换为我自己的值,如上面第一行代码中所示的“0、1、10、25、100、250、1000、10000”
# add include.lowest = TRUE, otherwise the 0 values will become NAs
map.df$brks <- cut(map.df$data1,
breaks = c(0, 1, 10, 25, 100, 250, 1000, 10000),
labels = c("0", "1 - 10", "11 - 25", "26 - 100",
"101 - 250", "251 - 1000", "1001 - 10000"),
include.lowest = TRUE)
# use scale_fill_brewer rather than scale_fill_gradientn; it expects
# a categorical variable (e.g. brks) while scale_fill_gradientn expects
# a continuous variable (e.g. data1).
# also, I suggest placing the legend at the side, as a vertical stack of
# boxes, which resembles a color bar.
# theme_void can be used to remove all backgrounds, axis labels, etc.
ggplot(map.df, aes(x=long, y=lat, group=group, fill=brks)) +
geom_polygon(colour = "grey", size = 0.001) +
coord_map() +
expand_limits(x = map.county$long, y = map.county$lat) +
scale_fill_brewer(palette = "YlOrRd") +
theme_void() +
theme(legend.position = "right")