如何从 ggplot2 图例中删除美感

发布于 2024-10-26 11:01:01 字数 1176 浏览 1 评论 0原文

我正在寻找一种方法来隐藏使用下面的代码创建的绘图中的一个审美传说。要按日期缩放点颜色,我必须将日期转换为数字,并且我不想在绘图上显示日期图例。另一方面,形状图例是要显示的重要信息。我知道 legend.position="none" 将完全删除图例,但这给我留下了如何传达形状背后的含义的问题。

library(ggplot2)
w<-read.table("data.txt", header=TRUE)
pt.data <- w[w$dt==min(w$dt),]
p <- ggplot(data=w, aes(OAD,RtgValInt,color=dt,shape=Port)) +
     geom_jitter(size=3, alpha=0.75) +
     scale_colour_gradient(limits=c(min(w$dt), 
             max(w$dt)),
         low="#9999FF", high="#000066") +
     geom_point(data=pt.data, 
         color="red", size=3, aes(shape=Port))
print(p)

data.txt 文件包含以下行。

Date          Port    OAD         RtgValInt   dt
12/31/2010  Grp1    1.463771    1.833333    14974
12/31/2010  Grp2    1.193307    2.071429    14974
11/30/2010  Grp1    1.454115    1.833333    14943
11/30/2010  Grp2    1.127755    2.071429    14943
10/29/2010  Grp1    1.434965    2.000000    14911
10/29/2010  Grp2    1.055758    2.071429    14911
09/30/2010  Grp1    1.441773    2.000000    14882
09/30/2010  Grp2    1.077799    2.071429    14882

在此处输入图像描述

I am looking for a way to hide one of the aestetic legends from the plot created with the code below. To scale the point color by date, I had to convert the dates into numbers, and I'd rather not show the date legend on the plot. On the other hand, the shape legend is important information to display. I understand that legend.position="none" will completely remove the legend, but then that leaves me with the problem of how to communicate the meaning behind the shapes.

library(ggplot2)
w<-read.table("data.txt", header=TRUE)
pt.data <- w[w$dt==min(w$dt),]
p <- ggplot(data=w, aes(OAD,RtgValInt,color=dt,shape=Port)) +
     geom_jitter(size=3, alpha=0.75) +
     scale_colour_gradient(limits=c(min(w$dt), 
             max(w$dt)),
         low="#9999FF", high="#000066") +
     geom_point(data=pt.data, 
         color="red", size=3, aes(shape=Port))
print(p)

The data.txt file includes the lines below.

Date          Port    OAD         RtgValInt   dt
12/31/2010  Grp1    1.463771    1.833333    14974
12/31/2010  Grp2    1.193307    2.071429    14974
11/30/2010  Grp1    1.454115    1.833333    14943
11/30/2010  Grp2    1.127755    2.071429    14943
10/29/2010  Grp1    1.434965    2.000000    14911
10/29/2010  Grp2    1.055758    2.071429    14911
09/30/2010  Grp1    1.441773    2.000000    14882
09/30/2010  Grp2    1.077799    2.071429    14882

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2024-11-02 11:01:01

您可以在图层级别抑制图例。如果将颜色美感从最初对 ggplot 的调用移至抖动层,这似乎会给出您想要的效果。我有点困惑为什么你想根据日期进行着色而不提供颜色含义的关键......但这是一个更哲学的问题需要你思考。

ggplot(data=w, aes(OAD,RtgValInt,shape=Port)) +
  geom_jitter(size=3, alpha=0.75, color=dt, legend = FALSE) +
  scale_colour_gradient(limits=c(min(w$dt), max(w$dt)),low="#9999FF", high="#000066") +
  geom_point(data=pt.data, color="red", size=3, aes(shape=Port))

You can suppress the legends at the layer level. If you move the colour aesthetic from the initial call to ggplot to the jitter layer, that seems to give the effect you are after. I'm a bit confused though as to why you would want to colour based on date and not provide the key as to what the colors mean...but that's a more philosophical question for you to ponder.

ggplot(data=w, aes(OAD,RtgValInt,shape=Port)) +
  geom_jitter(size=3, alpha=0.75, color=dt, legend = FALSE) +
  scale_colour_gradient(limits=c(min(w$dt), max(w$dt)),low="#9999FF", high="#000066") +
  geom_point(data=pt.data, color="red", size=3, aes(shape=Port))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文