【问题标题】:R - create plot grid and then plot data pointsR - 创建绘图网格,然后绘制数据点
【发布时间】:2015-04-10 12:41:37
【问题描述】:

我想知道是否可以仅在 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。感谢任何帮助。谢谢。

【问题讨论】:

    标签: r plot categories scatter-plot


    【解决方案1】:

    是的,你可以!

    只需创建一个空图,类型为“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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-25
      • 2023-04-02
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多