Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 删除一个特定几何图形的图例元素:";show.legend=FALSE";他不能胜任这项工作_R_Ggplot2 - Fatal编程技术网

R 删除一个特定几何图形的图例元素:";show.legend=FALSE";他不能胜任这项工作

R 删除一个特定几何图形的图例元素:";show.legend=FALSE";他不能胜任这项工作,r,ggplot2,R,Ggplot2,我已经写了一个答案,并希望改进它。 我想做的是删除geom_path的图例,但它不适用于show.legend=FALSE。geom_路径的color元素保留在图例中(Down和Up)。我做错什么了吗 是否有另一种方法可以手动告诉ggplot仅显示图例的最后两个元素(y2015,y2016) 我的代码和输出: library(ggplot2) library(reshape2) library(dplyr) ggplot2df <- read.table(text = "question

我已经写了一个答案,并希望改进它。 我想做的是删除
geom_path
的图例,但它不适用于
show.legend=FALSE
geom_路径的
color
元素保留在图例中(
Down
Up
)。我做错什么了吗

是否有另一种方法可以手动告诉
ggplot
仅显示图例的最后两个元素(
y2015
y2016

我的代码和输出:

library(ggplot2)
library(reshape2)
library(dplyr)

ggplot2df <- read.table(text = "question y2015 y2016
                        q1 90 50
                        q2 80 60
                        q3 70 90
                        q4 90 60
                        q5 30 20", header = TRUE)


df <- ggplot2df %>% 
  mutate(direction = ifelse(y2016 - y2015 > 0, "Up", "Down"))%>%
  melt(id = c("question", "direction"))


ggplot(df, aes(x=question, y = value, color = variable, group = question )) + 
geom_point(size=4) + 
geom_path(aes(color = direction), arrow=arrow(), show.legend = FALSE)  
库(ggplot2)
图书馆(E2)
图书馆(dplyr)
ggplot2df 0,“向上”、“向下”))%>%
熔化(id=c(“问题”、“方向”))
ggplot(df,aes(x=问题,y=值,颜色=变量,组=问题))+
几何点(尺寸=4)+
几何路径(aes(颜色=方向),箭头=箭头(),show.legend=FALSE)

我认为是这样的,因为
变量
方向
都映射到了颜色,所以图例有四个不同的颜色值。删除路径图例只会删除箭头,而删除点图例只会删除点。但无论哪种方式,所有四种颜色仍分别以点或箭头的形式显示在图例中,因为基础映射仍有四个值,而不管您选择在图例中以点、箭头或两者的形式显示这四个值

解决这个问题的一种方法是对点使用填充美学。那么路径图例将只有两个值。为此,必须使用具有填充内部的点样式(pch值21-25)。您还需要更改“颜色美学”或“填充美学”的颜色,以便它们不相同:

ggplot(df, aes(x=question, y = value, group = question)) + 
  geom_point(size=4, aes(fill=variable), pch=21, color=NA) + 
  geom_path(aes(color = direction), arrow=arrow(), show.legend=FALSE) +
  scale_fill_manual(values=hcl(c(105,285), 100, 50))

另一种可能性(但更全面的工作)是手动指定箭头的颜色:

library(ggplot2)
library(tidyr)
library(dplyr)

ggplot2df <- read.table(text = "question y2015 y2016
q1 90 50
q2 80 60
q3 70 90
q4 90 60
q5 30 20", header = TRUE)

ggplot2df %>% 
  mutate(direction = ifelse(y2016 - y2015 > 0, "Up", "Down")) %>%
  gather(variable, value, -question, -direction) -> df

gg <- ggplot(df, aes(x=question, y = value, group = question)) 
gg <- gg + geom_point(aes(color=variable), size=4) 
gg <- gg + geom_path(color=c("red", "red", "green", rep("red", 4), "green", "red", "red"),
                     arrow=arrow(), show.legend=FALSE)  
gg
库(ggplot2)
图书馆(tidyr)
图书馆(dplyr)
GG2DF%
突变(方向=ifelse(y2016-y2015>0,“向上”、“向下”))%>%
聚集(变量、值、问题、方向)->df

谢谢(和+1)!这是一个很好的解决办法。能否将
show.legend=FALSE
移动到
geom_path
以匹配问题。不过,我不会直接接受,因为我觉得肯定还有其他答案,因此我希望鼓励其他答案。是的,对不起。我实际上认为在这种特殊情况下,有更好的方法来编码信息(),但是当你想要创建两种不同颜色美学的等价物时,对点使用填充美学仍然是一个很好的技巧。我将研究它。谢谢