我收到一条重复的错误消息"CCD_ 1";即使我使用的是数字变量。
这是我的代码
name = c("Alberto Manguel", "Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel",)
year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999)
portion = c(0.0044117647, 0.0191846523, 0.0151898734, 0.0042075736, 0.0044493882, 0.0021881838, 0.0146396396, 0.0162689805, 0.0136054422, 0.0158730159)
df <- tibble(name, year, portion)
ggplot(df) + geom_line(aes(x = year, y = portion, group = name), colour="black") + geom_text(aes(year == 1999, label = name), hjust = -.1)
我的year
变量已经是一个数值,所以我不需要转换它?我缺少什么?
问题出在geom_text行。假设你想在对应的点上显示每个名字,所以把x和y的aes值放在ggplot中,与geom_line和geom_text共享,然后我们可以指定每个名字的美学。
library(ggplot2)
ggplot(df, aes(x = year, y = portion)) +
geom_line(aes(group = name), colour = "black") +
geom_text(aes(label = name), hjust = -0.1)
或者如果意图只是标记最后一点,那么:
library(dplyr)
library(ggplot2)
ggplot(df, aes(x = year, y = portion)) +
geom_line(aes(group = name), colour = "black") +
geom_text(aes(label = name), data = slice_tail, hjust = -0.1)
根据需要,slice_tail可以替换为其中一个——波浪号是data=参数的一部分,表示它正在定义一个函数,ggplot2将应用于定义中的点表示的df。
~ slice_max(., year) # all rows having the largest year
~ filter(., year == 1999) # all rows having year 1999
~ group_by(., name) %>% slice_max(year) %>% ungroup # row of max year in each name
备注
问题中显示的输入有错误,所以我们使用了这个:
name = c("Alberto Manguel", "Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel")
year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999)
portion = c(0.0044117647, 0.0191846523, 0.0151898734, 0.0042075736, 0.0044493882, 0.0021881838, 0.0146396396, 0.0162689805, 0.0136054422, 0.0158730159)
df <- data.frame(name, year, portion)
在这种情况下,X和Y都应该是数字?也是Y数字。如果你说你解决了这个问题,那么应该依赖ggplot((函数或某个层。
通过运行您的代码,我发现问题应该出现在geom_text((层。没有它,我们的情节就很好。如果你想在特定的点上得到名字,这对我有效(尽管我不确定这是否是你的意图!:
name <- c("Alberto Manguel", "Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel")
year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999)
portion = c(0.0044117647, 0.0191846523, 0.0151898734, 0.0042075736, 0.0044493882, 0.0021881838, 0.0146396396, 0.0162689805, 0.0136054422, 0.0158730159)
df <- data.frame(name, year, portion)
plot <- ggplot() + geom_line(aes(x = year, y = portion, group = name), colour="black") +
geom_text(aes(x=year, y=portion, label=name))
它可能是错误的,因为它将NAME作为一个离散变量,所以它不能真正用作变量本身,而只能用作标签?