문제

I have a data frame that comes from a lm subset composed of the intercept (ordenada) and the slope (velocidad1) calculated for each subject.

 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")

My goal is to get a barplot of the data in second row ( A[2,] ) splitting by group (UT which contains UT1,UT2... and TR) in the same graph. I am trying to do some ggplot but keep failing over and over again. I get no layers in plot error or margin error in base graphics.

The output should look like this

enter image description here

I KNOW the answer is in the reshape package but I wish there's another way to do that.

Thank you in advance.

도움이 되었습니까?

해결책

Using base graphics:

# 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)))

enter image description here

Possibly add a legend:

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

다른 팁

EDIT: Not using reshape per request in comments

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") 

Here is another option that incorporates your complete dataset. Not sure if this is usefull for you. I've used reshape2, it's actually easier. You just have to melt(yourdataframe), for your particular case there is no need to specify anything else in the melt function arguments.

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")

enter image description here

Using Functions

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))

I guess you could loop in order to plot several data frames using the same plot function. I guess you could probably addapt it more to your needs, but this can get you started

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top