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

R - create plot grid and then plot data points

我想知道是否可以在 R 中仅创建绘图的 "grid",然后根据矩阵中的值添加数据点。例如,我想在 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"))。然后我会在 Belgium - 1991 添加 point/circle/whatever。感谢任何帮助。谢谢。

是的,你可以!

只需创建一个空地块,类型为 "n"(对于 none)

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)