我有一个来自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")
我的目标是在同一图表中获得第二行(A [2,])中按组分组的数据(包含UT1,UT2 ...和TR的UT)的条形图。我正在尝试做一些ggplot,但一直都在失败。基本图形中的绘图错误或边距错误没有任何图层。
输出应该如下所示
我知道答案是在重塑包中,但我希望还有另一种方法可以做到这一点。
提前谢谢。
答案 0 :(得分:2)
使用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"))
答案 1 :(得分:1)
编辑:评论中每个请求未使用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")
答案 2 :(得分:1)
这是包含完整数据集的另一个选项。不确定这对你有用。
我使用了reshape2,它实际上更容易。您只需要melt(yourdataframe)
,对于您的特定情况,不需要在融合函数参数中指定任何其他内容。
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))
我猜你可以循环以便使用相同的绘图功能绘制几个数据帧。 我想你可能会更多地满足你的需求,但这可以让你开始