[英]How to connect points intervened by NA in ggplot in r?
In the following data, the y-values are intervened by NA values.在以下数据中,y 值由 NA 值干预。 When I try to plot the values in ggplot with the following codes, values intervened by NA are not connected.
当我尝试使用以下代码 plot ggplot 中的值时,NA 干预的值未连接。 How to connect them?
如何连接它们?
My data are我的数据是
> dput(dat)
structure(list(iso2c = c("", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", ""), country = c("Chile", "Chile", "Chile", "Chile",
"Chile", "Chile", "Chile", "Chile", "Chile", "Chile", "Chile",
"Chile", "Chile", "Chile", "Chile", "Mexico", "Mexico", "Mexico",
"Mexico", "Mexico", "Mexico", "Mexico", "Mexico", "Mexico", "Mexico",
"Mexico", "Mexico", "Mexico", "Mexico", "Mexico", "Uruguay",
"Uruguay", "Uruguay", "Uruguay", "Uruguay", "Uruguay", "Uruguay",
"Uruguay", "Uruguay", "Uruguay", "Uruguay", "Uruguay", "Uruguay",
"Uruguay", "Uruguay"), gini = structure(c(NA, 0.504536343101375,
NA, 0.508405328631639, NA, 0.519976764499864, NA, NA, 0.517926578610535,
NA, NA, 0.546104633218656, NA, NA, 0.555941876065133, 0.491403873718798,
NA, 0.493552378351658, NA, 0.475493191921101, NA, 0.505257097450159,
NA, 0.497832261107211, 0.511129547580954, 0.509540976787275,
NA, 0.512466806741829, NA, 0.539429975768021, 0.41795824834724,
0.420833068910888, 0.41522929739879, 0.435913025490141, 0.454821576286135,
0.46476558158648, 0.464685756891657, 0.47909997120289, 0.473975286071775,
0.458674509620144, 0.471347484288568, 0.462187124859573, 0.46655236747263,
0.461672318705308, 0.443881977304363), label = "Gini Coefficient"),
year = c(2014L, 2013L, 2012L, 2011L, 2010L, 2009L, 2008L,
2007L, 2006L, 2005L, 2004L, 2003L, 2002L, 2001L, 2000L, 2014L,
2013L, 2012L, 2011L, 2010L, 2009L, 2008L, 2007L, 2006L, 2005L,
2004L, 2003L, 2002L, 2001L, 2000L, 2014L, 2013L, 2012L, 2011L,
2010L, 2009L, 2008L, 2007L, 2006L, 2005L, 2004L, 2003L, 2002L,
2001L, 2000L)), row.names = c(NA, 45L), class = "data.frame")
The codes used are使用的代码是
ggplot(dat, aes(x = year, y = gini, colour = country)) +
geom_line() +
geom_point(aes(shape = country), size=3)+
scale_shape_manual(values=c(1:3))
The plot I get is我得到的 plot 是
You've to drop the missing values from the data frame.您必须从数据框中删除缺失值。 Use is.na() function to filter rows where gini is not NA.
使用 is.na() function 过滤 gini 不是 NA 的行。
df_nona <- dat[!is.na(dat$gini),]
ggplot(df_nona, aes(x = year, y = gini, colour = country)) +
geom_line() +
geom_point(aes(shape = country), size=3)+
scale_shape_manual(values=c(1:3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.