【问题标题】:Plot fitted data from data frame as side-by-side barplot将数据框中的拟合数据绘制为并排条形图
【发布时间】:2014-04-17 20:19:22
【问题描述】:

我有一个来自 lm 子集的数据框,该子集由为每个主题计算的截距 (ordenada) 和斜率 (velocidad1) 组成。

 A
                  UT1    UT2          UT3      UT4
    ordenada   1213.8 2634.8 3.760000e+02 -11080.8
    velocidad1    1.5   -2.5 6.615954e-14     20.0
                  UT5          UT6           UT7
    ordenada   1711.8 1.739000e+03  1.800000e+01
    velocidad1   -2.5 5.039544e-14 -9.154345e-16
                  UT8   UT9   UT10  UT11   UT12
    ordenada   5659.2 -2791 3422.6 418.2 2802.2
    velocidad1   -6.0     5   -1.0  -0.5   -1.5
                       UT13   UT14      TR1     TR2
    ordenada   2.832000e+03 -411.2 -15722.0 -1105.4
    velocidad1 1.405114e-13    3.5     25.5    25.0
                        TR3   TR4    TR5          TR6
    ordenada    1.14600e+03 299.6 1943.4 6.840000e+02
    velocidad1 -5.11402e-14   2.0   -2.5 6.479414e-14
                 TR7           TR8     TR9    TR10
    ordenada   354.8  1.317000e+03 33284.6 -3742.6
    velocidad1   1.0 -3.475548e-14   -52.0     8.0
                        TR11   TR12    TR13
    ordenada    7.400000e+02 2205.4 -4542.6
    velocidad1 -8.018585e-14   -2.5     8.0
                        TR14
    ordenada    5.880000e+02
    velocidad1 -4.406498e-14


dput(A)
structure(list(UT1 = c(1213.79999999971, 1.50000000000047), UT2 = c(2634.80000000021, 
-2.50000000000033), UT3 = c(375.999999999959, 6.61595351840473e-14
), UT4 = c(-11080.8000000008, 20.0000000000013), UT5 = c(1711.80000000007, 
-2.50000000000012), UT6 = c(1738.99999999997, 5.03954433109254e-14
), UT7 = c(18.0000000000006, -9.15434469010036e-16), UT8 = c(5659.20000000026, 
-6.00000000000041), UT9 = c(-2791.00000000024, 5.00000000000039
), UT10 = c(3422.59999999968, -0.99999999999948), UT11 = c(418.199999999958, 
-0.499999999999932), UT12 = c(2802.20000000017, -1.50000000000028
), UT13 = c(2831.99999999991, 1.40511433073812e-13), UT14 = c(-411.200000000294, 
3.50000000000048), TR1 = c(-15722.0000000017, 25.5000000000028
), TR2 = c(-1105.40000000264, 25.0000000000043), TR3 = c(1146.00000000003, 
-5.11402035568996e-14), TR4 = c(299.599999999803, 2.00000000000032
), TR5 = c(1943.40000000013, -2.50000000000021), TR6 = c(683.99999999996, 
6.47941413997612e-14), TR7 = c(354.800000000011, 0.999999999999982
), TR8 = c(1317.00000000002, -3.47554781454658e-14), TR9 = c(33284.6000000025, 
-52.000000000004), TR10 = c(-3742.60000000058, 8.00000000000094
), TR11 = c(740.00000000005, -8.0185853149896e-14), TR12 = c(2205.40000000021, 
-2.50000000000034), TR13 = c(-4542.60000000042, 8.00000000000067
), TR14 = c(588.000000000027, -4.40649812201441e-14)), .Names = c("UT1", 
"UT2", "UT3", "UT4", "UT5", "UT6", "UT7", "UT8", "UT9", "UT10", 
"UT11", "UT12", "UT13", "UT14", "TR1", "TR2", "TR3", "TR4", "TR5", 
"TR6", "TR7", "TR8", "TR9", "TR10", "TR11", "TR12", "TR13", "TR14"
), row.names = c("ordenada", "velocidad1"), class = "data.frame")

我的目标是在同一张图中按组(包含 UT1、UT2... 和 TR 的 UT)在第二行( A[2,] )中获取数据的条形图。我正在尝试做一些 ggplot 但一遍又一遍地失败。我在基本图形中没有图层错误或边距错误。

输出应该是这样的

我知道答案在 reshape 包中,但我希望有另一种方法来做到这一点。

提前谢谢你。

【问题讨论】:

  • 按组拆分是什么意思?你能更详细地描述一下最终的情节应该是什么样子吗?
  • 当然,让我编辑我的问题。我会把它画在别的地方来说明我的意思。
  • dput您的数据框。
  • 这篇关于 tidy data 的论文是 ggplot 期望您的数据用于绘图的格式。

标签: r ggplot2 bar-chart


【解决方案1】:

使用base 图形:

# convert the one-row data frame to a two-row matrix
m <- matrix(unlist(df[2, ]), nrow = 2, byrow = TRUE)

# plot
barplot(m, beside = TRUE, col = c("blue", "red"), names.arg = seq_len(ncol(m)))

可能添加一个图例:

legend("topright", legend = c("UT", "TR"), fill = c("blue", "red"))

【讨论】:

    【解决方案2】:

    编辑:在 cmets 中每个请求不使用 reshape

    library(ggplot2)
    plot_data <- data.frame(names(A), t(A[2,]))
    names(plot_data) <- c("variable", "value")
    plot_data$group <- grepl("^TR", plot_data$variable)
    plot_data$variable <- gsub("[^0-9]", "", as.character(plot_data$variable))
    plot_data$variable <- factor(plot_data$variable, 
                                 unique(sort(as.numeric(plot_data$variable))))
    p <- ggplot(aes(y = value, x = variable, fill = group), data = plot_data)
    p + geom_bar(stat = "identity", position = "dodge") 
    

    【讨论】:

    • 我试图避免重塑,因为我有很多情节要做,而且我的代码会太大。如果我以后想改变一些东西,我可能会忘记路上的东西
    【解决方案3】:

    这是另一个包含完整数据集的选项。不确定这是否对您有用。 我用过reshape2,它实际上更容易。您只需 melt(yourdataframe),对于您的特定情况,无需在 melt 函数参数中指定任何其他内容。

    require("ggplot2")
    require("reshape2")
    
    A <- df
    df1 <- melt(df[1,])
    df1$origen <- "ORDENADA"
    df2 <- melt(df[2,])
    df2$origen <- "VELOCIDAD"
    identical(df1$variable,df2$variable)
    
    df3 <- rbind(df1,df2)
    df3$group <- ifelse(grepl("^TR", df3$variable) == TRUE, "TR", "UT")
    df3$vble <- gsub("[^0-9]", "", as.character(df3$variable))
    df3$vble <- factor(df3$vble, levels = as.numeric(unique(df3$vble)))
    
    ggplot(aes(y = value, x = vble, fill = group), data = df3) +
      geom_bar(stat = "identity", position = "dodge") +
      facet_grid(origen ~ ., scales = "free")
    

    使用函数

    prepare <- function(data){
      data1 <- melt(data[1,])
      data1$origen <- "ORDENADA"
      data2 <- melt(data[2,])
      data2$origen <- "VELOCIDAD"
      identical(data1$variable,data2$variable)
      data3 <- rbind(data1,data2)
      data3$group <- ifelse(grepl("^TR", data3$variable) == TRUE, "TR", "UT")
      data3$vble <- gsub("[^0-9]", "", as.character(data3$variable))
      data3$vble <- factor(data3$vble, levels = as.numeric(unique(data3$vble)))
      return(data3)
    }
    
    prepare(df)
    
    #This would work, but is a bit manual for many plots:
    
    ggplot(aes(y = value, x = vble, fill = group), data = prepare(df)) +
          geom_bar(stat = "identity", position = "dodge") +
          facet_grid(origen ~ ., scales = "free")
    
    
    plot_fun <- function(data){
      p <- ggplot(data, aes_string(x = "vble", y = "value", fill = "group"))
      p <- p + geom_bar(stat = "identity", position = "dodge")
      p <- p + facet_grid(origen ~ ., scales = "free")
      suppressWarnings(print(p))
    }
    
    plot_fun(prepare(df))
    

    我猜你可以循环使用相同的绘图函数来绘制多个数据框。 我想您可能会根据自己的需要添加更多内容,但这可以帮助您入门

    【讨论】:

    • 我喜欢你的回答,虽然它使用了重塑。我想我可以编写一个函数,在被问到时将任何数据框与 df 匹配,所以我不应该担心我的 A、B、C、D 等的大小。因为你写的代码只使用了变量。类似 function(data) if(data="A") A
    • 我已经编辑了我的答案,添加了一些功能。幸运的是 ggplot 提供了 aes_string 函数来使这更容易。注意:我叫 df 你叫 A。
    猜你喜欢
    • 1970-01-01
    • 2016-05-16
    • 2022-11-23
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2019-04-16
    相关资源
    最近更新 更多