R:ggplot2:图例重叠了不同的点形状 [英] R: ggplot2: Legend overlaps different point shapes

查看:549
本文介绍了R:ggplot2:图例重叠了不同的点形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如您所看到的,图例与下面的图中的不同点形状重叠

  europe <-c(70,67 ,56,66,80,75,81,55)
data < - data.frame(europe)

ggplot(data,aes(x = 4,y = europe)) +
labs(x =Europe,y = NULL,title = NULL)+
geom_errorbar(y = data $ europe,ymin = min(data $欧洲),ymax = max(欧元),size = 1.5,width = 1,color =#7F7F7F)+
geom_point(aes(x = 4,y = data $ europe [1],color =today),size = 18,形状= 18)+
geom_point(aes(x = 4,y = data $ europe [6],color =last week),size = 16)+
scale_y_continuous(limits = c(50 (0,0),expand = c(0,0),expand = c(0,0),breaks = seq(0,1000,5))+
scale_x_continuous(limits = c(0,8)
scale_color_manual(name = element_blank(),
labels = c(today,last week),
values = c(#A50021,#00669C))



所以我一直尝试不成功通过添加

  + scale_shape_manual(name = element_blank(),
labels = c(today ,上周),
values = c(18,16))

什么都不会改变什么。当然,我的理想将是一个红色圆圈和一个蓝色方块。有人可以帮我解决这个问题吗?

解决方案

在数据中,因为一般情况下,当它们映射到使用aes的数据时,它们的效果最好。这就是ggplot的易用性:在数据框中获取所有内容,设置aes,如果您想要,可以使用 scale _...._手册来更改默认值。



我已经为您的数据添加了周日变量,以便能够正确使用aes。

  #add weekday variable 
data $ weekday< - c(today,last week)

ggplot(data,aes(x = 4,y =欧洲))+
labs(x =Europe,y = NULL,title = NULL)+
geom_errorbar(aes(y =欧洲,ymin = min(欧洲),ymax = max )),size = 1.5,width = 1,color =#7F7F7F)+
#在这里添加数据子集。不是x和y已经被映射,并且颜色,形状和大小被映射
#INSIDE aes
geom_point(data = data [c(1,6),],aes(color = weekday,shape =周日,大小=周日))+
#手动比例
scale_color_manual(values = c(today=#A50021,last week=#00669C))+
scale_size_manual(values = c(today= 18,last week= 16))+
scale_shape_manual(values = c(today= 20,last week= 18))



<编辑删除错误栏:

 #为某个数据框重新绘制/操作以绘制

datadata< - data.frame(value = europe [c(1,6)],
weekday = c(today,last week),
limit_value = c(min(europe ),max(欧洲)),
limit_label = c(min,max))

p2 < - ggplot(plotdata)+
geom_point(aes(x = 4,y = value,shape = weekday,color = weekday,size = weekday))+
scale_color_manual(values = c today=#A50021,
last week=#00669C))+
scale_size_manual(values = c(today= 18,last week= 16))+
scale_shape_manual(values = c(today= 18,last week= 20))+
geom_hline(aes(yintercept = limit_value),color =gray,size = 2)
p2



或者,因为时间间隔也很重要,所以可以这样做。它使时间关系更清晰。在我看来,min和max本身就很明显。如果没有,您可以随时添加文本标签(或者将线条变成红色或蓝色或其他)。


as you can see, the legend overlaps different point shapes in following plot

europe <- c(70, 67, 56, 66, 80, 75, 81, 55)
data <- data.frame(europe)

ggplot(data, aes(x=4, y=europe)) +
  labs(x = "Europe", y = NULL, title = NULL) +  
  geom_errorbar(y=data$europe, ymin=min(data$europe), ymax=max(data$europe), size=1.5, width=1, color="#7F7F7F") +
  geom_point(aes(x=4, y=data$europe[1], color="today"), size=18, shape=18) +
  geom_point(aes(x=4, y=data$europe[6], color="last week"), size=16) +
  scale_y_continuous(limits=c(50, 85), expand = c(0, 0), breaks=seq(0, 1000, 5)) +
  scale_x_continuous(limits=c(0, 8), expand = c(0, 0)) +
  scale_color_manual(name = element_blank(),
                 labels = c("today", "last week"),
                 values = c("#A50021", "#00669C"))

so I have been unsuccessfully trying to fix the overlap such as by adding

+ scale_shape_manual(name = element_blank(),
                 labels = c("today", "last week"),
                 values = c(18, 16))

what does not change anything. my ideal would be, of course, one red circle and one blue square. can someone help me out with this?

解决方案

You are making things very difficult for yourself by adding things to the legend that are not in the data, because generally legends work best when they are mapped to the data using the aes. That's the ease of ggplot: get everything in your dataframe, set aes and if you want to you can change the defaults by using scale_...._manual.

I have added a 'weekday' variable to your data, to be able to use the aes correctly.

#add weekday variable
data$weekday <- c("today","last week")

ggplot(data, aes(x=4, y=europe)) +
  labs(x = "Europe", y = NULL, title = NULL) +  
  geom_errorbar(aes(y=europe, ymin=min(europe), ymax=max(europe)), size=1.5, width=1, color="#7F7F7F") +
  #add subset of data here. Not that x and y were already mapped, and color, shape and size are mapped 
  #INSIDE aes
  geom_point(data=data[c(1,6),], aes(color=weekday, shape=weekday, size=weekday))+
  #manual scales
  scale_color_manual(values = c("today"="#A50021", "last week"="#00669C")) +
  scale_size_manual(values=c("today"=18, "last week"=16)) +
  scale_shape_manual(values=c("today"=20,"last week"=18))

Edit with removing the errorbar:

#do some reshaping/manipulating into a dataframe for plotting

plotdata <- data.frame(value=europe[c(1,6)],
                       weekday=c("today","last week"),
                       limit_value=c(min(europe),max(europe)),
                       limit_label=c("min","max"))

p2 <- ggplot(plotdata) +
  geom_point(aes(x=4,y=value,shape=weekday,colour=weekday, size=weekday))+
  scale_color_manual(values=c("today"="#A50021",
                              "last week"="#00669C"))+
  scale_size_manual(values=c("today"=18,"last week"=16))+
  scale_shape_manual(values=c("today"=18, "last week"=20))+
  geom_hline(aes(yintercept=limit_value),colour="grey",size=2)
p2

Or, because time-intervals can also be important you can do this. It makes the time-relations clearer. In my opinion, min and max are clear by themselves. If not, you can always add a text label (or make the lines red and blue or something).

这篇关于R:ggplot2:图例重叠了不同的点形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆