【问题标题】:Representation with, scale_fill_gradientn ggplot2, constant gradient用 scale_fill_gradientn ggplot2 表示,恒定梯度
【发布时间】:2017-11-14 08:52:50
【问题描述】:

我有以下代码来创建带有ggplot2 的极坐标图,但是当我使用其他数据运行相同的代码时,我无法保持相同的比例颜色值。

有3个不同的变量来表示,一个是方向:

plot.new()
ggplot(NS_Enero, aes(x = wd, y = ws, fill = manganese, size = manganese)) +
  coord_polar() +
  geom_point(shape = 21, show.legend = TRUE) +
  scale_size(range = c(3,12),
             labels = c("50", "150", "450", "1350", "4050"),
             breaks = c(50, 150, 450, 1350, 4050),
             name = expression(paste(PM[10]~bound~Mn~(ng/m^3)))) +
  scale_fill_gradientn(colours = c("darkblue","blue", "yellow", "orange", "red"),
                       space = "Lab",
                       guide = "legend",
                       values = rescale(c(0, 150, 450, 1350, 4401),
                                        from = c(0, 4401)),
                       labels = c("50", "150", "450", "1350", "4050"),
                       breaks = c(50, 150, 450, 1350, 4050),
                       name = expression(paste(PM[10]~bound~Mn~(ng/m^3)))) +
  scale_x_continuous(limits= c(0,360),
                     breaks= c(0, 90, 180, 270), 
                     labels = c("N","E","S","W"),
                     name = "") +
  scale_y_continuous(name = "Distance (m)",
                     position = "left") +
  theme_linedraw() +
  theme(axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 8)) +
  ggtitle("                 NS_Enero")

当我尝试使用不同的数据运行相同的代码时,输​​出图会改变比例的值,我尝试了很多在其他问题中发现的提示,但我无法解决比例表示的问题.我认为问题出在values 来自scale_fill_gradientn...

输出 1:

输出 2(不同的数据):

最终结果没问题,但我希望比例范围(大小和颜色)的值在我使用不同数据范围运行的所有图中保持不变。对于 0 到 4050 之间的值,大小和颜色的值相同。

非常感谢您的帮助。

【问题讨论】:

  • 也许在scale_fill_gradientn 中指定limits
  • 天哪,这很简单,但是很有效,非常感谢您的帮助。

标签: r ggplot2 scale gradient polar-coordinates


【解决方案1】:

最后,添加limits 成功了。

这是最终代码:

#Tamaño y color
plot.new()
ggplot(NS_Junio, aes(x = wd, y = ws, fill = manganese, size = manganese)) +
  coord_polar() +
  geom_point(shape = 21, show.legend = TRUE) +
  scale_size(range = c(3,12),
             labels = c("50", "150", "450", "1350", "4050"),
             breaks = c(50, 150, 450, 1350, 4050),
             limits = c(1,4050),
             name = expression(paste(PM[10]~bound~Mn~(ng/m^3)))) +
  scale_fill_gradientn(colours = c("darkblue","blue", "yellow", "orange", "red"),
                       space = "Lab",
                       guide = "legend",
                       values = rescale(c(0, 150, 450, 1350, 4401),
                                        from = c(0, 4401)),
                       labels = c("50", "150", "450", "1350", "4050"),
                       breaks = c(50, 150, 450, 1350, 4050),
                       limits = c(1,4050),
                       name = expression(paste(PM[10]~bound~Mn~(ng/m^3)))) +
  scale_x_continuous(limits= c(0,360),
                     breaks= c(0, 90, 180, 270), 
                     labels = c("N","E","S","W"),
                     name = "") +
  scale_y_continuous(name = "Distance (m)",
                     position = "left") +
  theme_linedraw() +
  theme(axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 8)) +
  ggtitle("                 NS_Enero")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2022-05-16
    • 2021-03-08
    相关资源
    最近更新 更多