代码之家  ›  专栏  ›  技术社区  ›  Jack Armstrong

在R中用多列绘制表[重复]

  •  -1
  • Jack Armstrong  · 技术社区  · 6 年前

    这个问题已经有了答案:

    我有一个数据帧,我想绘制一个表,它使用R的图形简单地显示表中的输出:

    看起来是这样,但在图形窗口中:

    Letter   Color   Age  Height
      A        10      7     11 
      B        8       6     10
      C        9       5     4
    

    我想我可以用:

    ggplot(df,aes(x=colnames(df))+geom_bar('identity'))
    

    但是我应该用什么来填充表上的y坐标/值呢?

    1 回复  |  直到 6 年前
        1
  •  3
  •   akrun    6 年前

    gather geom_bar

    library(tidyverse)
    library(ggplot2)
    df %>% 
      gather(key, val, -Letter) %>% 
      ggplot(aes(x = Letter, y = val, fill = key)) + 
                 geom_bar(stat = 'identity')
    

    base R

    barplot(`colnames<-`(t(df[-1]), df$Letter), legend = TRUE,
                  col = c('red', 'blue', 'green'))
    

    df <- structure(list(Letter = c("A", "B", "C"), Color = c(10L, 8L, 
    9L), Age = c(7L, 6L, 5L), Height = c(11L, 10L, 4L)), .Names = c("Letter", 
    "Color", "Age", "Height"), class = "data.frame", row.names = c(NA, 
     -3L))