简体   繁体   English

geom_point和coord_polar-未知美学警告

[英]geom_point and coord_polar - Warning for unknown aesthetics

I know geom_point doesn't have mapping for ymin and ymax, but when I don't use them it doesn't give the figure I expect. 我知道geom_point没有ymin和ymax的映射,但是当我不使用它们时,它没有给出我期望的数字。 Nevertheless when using ymin and ymax I get the figure I wish, but I get warning of "Ignoring unknown aesthetics: ymin, ymax". 不过,当使用ymin和ymax时,我得到了想要的数字,但是我收到了“忽略未知的美学:ymin,ymax”的警告。 I can't figure out what should be done to avoid warning but getting the plot I wish. 我不知道应该怎么做才能避免警告,但得到我希望的情节。

Here is example dataset: 这是示例数据集:

ab <- data.frame(a = seq(50,100,by=10), b=LETTERS[1:6])

Without ymin and ymax but not the figure I expect: 没有ymin和ymax,但没有我期望的数字:

library("ggplot2")

ggplot(ab) +
  scale_x_discrete() +
    scale_y_reverse() +
  geom_rect(xmin=Inf, xmax = -Inf, ymin = 0 - 50, ymax = 0, fill="lightblue") +
  geom_rect(xmin=Inf, xmax = -Inf, ymin = 0 - 82, ymax = 0 - 50, fill="lightgreen") +
  geom_rect(xmin=Inf, xmax = -Inf, ymin = -100, ymax = 0 - 80, fill="darkgreen") +
  geom_vline(xintercept=1:6, size=1.5, color="white") +
  geom_hline(yintercept=c(50, 80, 100), size=0.1, color="white") +
  geom_point(aes(x=b, y=a),
             shape=21, fill="#FF9933", size=10, position="identity") +
  ggtitle("Test") +
  coord_polar() +
  theme(
    plot.title=element_text(face = "bold", color = "black", size = 17, ),
    panel.background=element_rect(fill = c("white")),
    panel.grid=element_blank(),
    panel.grid.major=element_line(size=2),
    panel.grid.minor.y=element_blank(),
    axis.text.x=element_text(vjust=5),
    axis.text=element_text(size = 12, color = "black", face = "bold"),
    axis.text.y=element_blank(),
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    axis.ticks=element_blank())

with ymin and ymax in geom_point (the expected output with warning): 在geom_point中使用ymin和ymax(带有警告的预期输出):

ggplot(ab) +
  scale_x_discrete() +
    scale_y_reverse() +
  geom_rect(xmin=Inf, xmax = -Inf, ymin = 0 - 50, ymax = 0, fill="lightblue") +
  geom_rect(xmin=Inf, xmax = -Inf, ymin = 0 - 82, ymax = 0 - 50, fill="lightgreen") +
  geom_rect(xmin=Inf, xmax = -Inf, ymin = -100, ymax = 0 - 80, fill="darkgreen") +
  geom_vline(xintercept=1:6, size=1.5, color="white") +
  geom_hline(yintercept=c(50, 80, 100), size=0.1, color="white") +
  geom_point(aes(x=b, y=a, ymin = 0, ymax = 100),
             shape=21, fill="#FF9933", size=10, position="identity") +
  ggtitle("Test") +
  coord_polar() +
  theme(
    plot.title=element_text(face = "bold", color = "black", size = 17, ),
    panel.background=element_rect(fill = c("white")),
    panel.grid=element_blank(),
    panel.grid.major=element_line(size=2),
    panel.grid.minor.y=element_blank(),
    axis.text.x=element_text(vjust=5),
    axis.text=element_text(size = 12, color = "black", face = "bold"),
    axis.text.y=element_blank(),
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    axis.ticks=element_blank())

In ggplot2, the data that you're plotting is what determines the plot area unless you explicitly change the plot limits. 在ggplot2中,除非您显式更改绘图限制,否则您要绘制的数据将决定绘图区域。 Your a variable (which you're plotting on the y-axis) only covers the range from 50 to 100. Thus, unless you specify a different y range to use in the plot, that's all you're seeing by default. a变量(在y轴上绘制)仅覆盖50到100的范围。因此,除非您指定要在图中使用的其他y范围,否则默认情况下就是这样。 In short, the proper way to fix this is to pass the limits argument to the scale_y_reverse call -- this tells the plot to plot a different range than the range the data covers. 简而言之,解决此问题的正确方法是将limits参数传递给scale_y_reverse调用-这告诉绘图绘制的范围与数据覆盖的范围不同。 It will look like: 它看起来像:

scale_y_reverse(limits=c(100, 0))

(The 100 comes first because you're reversing your y-axis.) (前100位是因为您要反转y轴。)

If you add that change to your plot without the ymin and ymax arguments, you'll get the graph you expect. 如果将更改添加到不带 yminymax参数的绘图中则将获得期望的图形。

Now, if ymin and ymax aren't mapped, why are they changing your plot? 现在,如果未映射yminymax ,为什么它们会更改您的图? I think the reason is that geometries (like geom_point ) don't remove aesthetic mappings that they don't understand. 我认为原因是几何图形(例如geom_point )不会删除他们不了解的美学映射。 These mappings will get carried down to the layer, and if the layer understands the mappings, it will use them. 这些映射将被带到该层,如果该层理解了这些映射,它将使用它们。 That essentially "hacks" y-axis limits into the plot without specifying them the right way. 这实际上是在不指定正确方式的情况下将y轴限制“侵入”绘图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM