Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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 “财产”;“颜色”;在aes()中,用于ggplot的;几何线();我的ggplot代码的一部分_R - Fatal编程技术网

R “财产”;“颜色”;在aes()中,用于ggplot的;几何线();我的ggplot代码的一部分

R “财产”;“颜色”;在aes()中,用于ggplot的;几何线();我的ggplot代码的一部分,r,R,给定以下数据帧: structure(list(Event = c(NA, "Safari Zone Europe", "Community Day", "Shiny Lugia Raid", "Community Day", NA, "Community Day", NA, "Community Day", NA, NA, "Dortmund Safari Zone", "Dortmund Safari Zone", "Community Day", "Chicago Go Fest",

给定以下数据帧:

structure(list(Event = c(NA, "Safari Zone Europe", "Community Day", 
"Shiny Lugia Raid", "Community Day", NA, "Community Day", NA, 
"Community Day", NA, NA, "Dortmund Safari Zone", "Dortmund Safari Zone", 
"Community Day", "Chicago Go Fest", "Chicago Go Fest", "Chicago Go Fest", 
"Zapdos Raid Day", NA, NA), Pokémon = c("Magikarp", "Magikarp, Pikachu", 
"Dratini, Swablu", "Lugia", "Mareep", "Magikarp", "Charmander", 
"Pichu", "Larvitar", "Wailmer", "Mawile", "Roselia", "Roselia", 
"Squirtle, Squirtle (sunglasses)", "Minun, Shuppet", "Plusle", 
"Minun", "Zapdos", "Mawile", "Swablu"), Date = structure(c(1502323200, 
1507334400, 1519430400, 1522368000, 1523750400, 1526515200, 1526688000, 
1527638400, 1529107200, 1529452800, 1529971200, 1530403200, 1530662400, 
1531008000, 1531526400, 1531612800, 1531699200, 1532131200, 1532649600, 
1533513600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Catches = c(1, 7, 9, 1, 15, 1, 4, 1, 8, 1, 1, 2, 1, 4, 3, 
    2, 1, 1, 1, 1), `Accumulated catches` = c(1, 8, 17, 18, 33, 
    34, 38, 39, 47, 48, 49, 51, 52, 56, 59, 61, 62, 63, 64, 65
    )), .Names = c("Event", "Pokémon", "Date", "Catches", "Accumulated catches"
), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
))
当我试图用x轴上的“日期”和y轴上的“累计捕获量”绘制数据时,我使用以下ggplot代码:

ggplot(Shiny_List, aes(x=`Date`, y=`Accumulated catches`)) +
  ggtitle("Shiny catches (dates based on GMT+1)") +
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_point() +
  geom_line() +
  geom_text(aes(label=Pokémon),hjust=0, vjust=1.0)
这给了我每个数据点之间的一条线

但是,接下来,我想根据数据框中的“事件”列来区分一些数据点,因此我将
color=Event
添加到ggplot的
aes()
部分,如下所示:

ggplot(Shiny_List, aes(x=`Date`, y=`Accumulated catches`, color=Event)) +
  ggtitle("Shiny catches (dates based on GMT+1)") +
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_point() +
  geom_line() +
  geom_text(aes(label=Pokémon),hjust=0, vjust=1.0)
由此产生的结果会破坏数据点之间的线。它现在为“事件”列中的每个唯一值创建单独的行,而不是像前面的图中那样在所有数据点之间仅创建一行

有没有什么方法可以让我保留不同数据点的颜色,并且让R忽略
geom_line()
部分的颜色,这样它就可以像开始一样为所有数据点创建一条线


作为记录,我也尝试了
geom_path()
,但没有任何改变。

一般来说,最好将属性映射到geom,而不是顶级
ggplot
对象。这允许更精细的控制

在这种情况下,将颜色映射到点

ggplot(Shiny_List, aes(x = `Date`, 
                       y = `Accumulated catches`)) +
ggtitle("Shiny catches (dates based on GMT+1)") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_point(aes(color=Event)) +
geom_line() +
geom_text(aes(label = Pokémon), 
              hjust = 0, 
              vjust = 1.0)

通常,最好将属性映射到几何体,而不是顶级
ggplot
对象。这允许更精细的控制

在这种情况下,将颜色映射到点

ggplot(Shiny_List, aes(x = `Date`, 
                       y = `Accumulated catches`)) +
ggtitle("Shiny catches (dates based on GMT+1)") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_point(aes(color=Event)) +
geom_line() +
geom_text(aes(label = Pokémon), 
              hjust = 0, 
              vjust = 1.0)

我认为您只需要将颜色映射添加到
geom_point
,而不是添加到
ggplot
。也就是说:
+geom_point(aes(color=Event))
@neilfws确实做到了!非常感谢。好。我将添加它作为答案。我认为您只需要将颜色映射添加到
geom_point
,而不是添加到
ggplot
。也就是说:
+geom_point(aes(color=Event))
@neilfws确实做到了!非常感谢。好。我将补充这一点作为答案。