繁体   English   中英

R-创建绘图网格,然后绘图数据点

[英]R - create plot grid and then plot data points

我想知道是否有可能仅在R中创建图的“网格”,然后根据矩阵中的值添加数据点。 例如,我想在X轴上有一些日历年值,在Y轴上有一些国家的名字。 然后,根据矩阵中的数据,在需要的地方在图形中添加一个点。 示例数据: Y_labels = c("Austria", "Belgium", "Germany", "Spain")X_labels = c(1990, 1991, 1992, 1993, 1994, 1995) 假设包含要绘制的数据点的向量是x = cbind(c(1991, 1993, 1995),c("Belgium", "Spain", "Belgium")) 然后,在1991年比利时,我将添加一个点/圆/任何位置。感谢您的帮助。 谢谢。

是的你可以!

只需创建一个类型为“ n”的空图(无)

df <- data.frame(year = c(1992, 1995, 1998, 1999), 
                 country = c("Austria", "Spain", "Spain", "Germany"))

# All the possible countries
all.countries <- c("Austria", "Belgium", "Germany", "Spain");
# Convert df$country to a factor
df$country <- factor(df$country, levels=all.countries)
# yaxt="n" hides the y axis, be sure to specify xlim and ylim
# so that your data fits in the graph!
plot(0, t="n", xlim=c(1990, 2000), ylim=c(1, length(all.countries)), 
     yaxt="n", xlab="Year", ylab="Country")
# Plot a y axis
axis(2, at=1:length(all.countries), labels=all.countries)
# points plots over an existing graph
points(df, pch=20)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM