如何遍历列名并制作一个组合所有列的ggplot散点图

时间:2016-07-21 12:16:29

标签: r plot ggplot2 knitr

我在数据框中的列中放置了几个清晰的测量值。 有时扫描即40蜱,但大多数是256蜱(8位)。 列始终按正确的顺序排列。

我的脚本从导入的excel文件中配置,告诉它使用knitr制作并收集到word doc或pdf中的图表。

现在我想在数据框中绘制几个cols并将它们组合在一个图中。

示例:

col 77是x

col 338至368是y

col 76用于对点进行颜色分级。

示例df:

temperature_set  VDD_set  RCAL01  RCAL02 RCAL03
             15      3.3     -97     -98   -103
             15      3.3     -97     -98   -103
             15      3.3     -97     -98   -103
             15      3.3     -97     -98   -103
             15      3.3     -97     -98   -103
             15      3.3     -97     -98   -103

此示例仅生成包含第一次和最后一次迭代的图:

myplot <- qplot(df[[77]], 
                df[[338]])
for(i in 339:368){
  myplot <- myplot + geom_point(aes(x = df[[77]],
                                    y = df[[i]], 
                                    color = df[[76]]))
}
print(myplot)

此示例仅绘制最后一次迭代:

myplot <- ggplot(df)
for(i in 338:368){
  myplot <- myplot + geom_point(aes(x = df[[77]],
                                    y = df[[i]], 
                                    color = df[[76]]))
}
print(myplot)

这项工作,但编码不漂亮,可扩展或实用:

ggplot(df, aes(df[[77]], df[,338])) + 
  geom_point(aes(x=df[[77]],y=df[,339], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,340], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,341], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,342], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,343], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,344], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,345], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,346], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,347], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,348], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,349], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,350], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,351], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,352], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,353], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,354], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,355], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,356], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,357], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,358], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,359], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,360], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,361], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,362], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,363], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,364], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,365], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,366], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,367], color = df[,76] )) +
  geom_point(aes(x=df[[77]],y=df[,368], color = df[,76] ))

这种方式有效,但情节不会扩展轴,ggplot更时尚:o)

plot(Data_Char[[77]], Data_Char[,338])
for(i in 338:368){
  points(Data_Char[[77]], Data_Char[,i])
}

在使用ggplot进行绘图之前是否需要重新排列数据?或者是否有没有?

的解决方案

1 个答案:

答案 0 :(得分:0)

最佳选择,使用tidyr::gather将数据从宽到长重新格式化。以下是使用mpg包中的ggplot2数据集的示例。

library(ggplot2)
library(tidyr)

#' reshape the data from wide to long.  Example the mpg data set from ggplot2

mpg 
# # A tibble: 234 × 11
#    manufacturer      model displ  year   cyl      trans   drv   cty   hwy    fl   class
#           <chr>      <chr> <dbl> <int> <int>      <chr> <chr> <int> <int> <chr>   <chr>
# 1          audi         a4   1.8  1999     4   auto(l5)     f    18    29     p compact
# 2          audi         a4   1.8  1999     4 manual(m5)     f    21    29     p compact
# 3          audi         a4   2.0  2008     4 manual(m6)     f    20    31     p compact
# 4          audi         a4   2.0  2008     4   auto(av)     f    21    30     p compact
# 5          audi         a4   2.8  1999     6   auto(l5)     f    16    26     p compact
# 6          audi         a4   2.8  1999     6 manual(m5)     f    18    26     p compact
# 7          audi         a4   3.1  2008     6   auto(av)     f    18    27     p compact
# 8          audi a4 quattro   1.8  1999     4 manual(m5)     4    18    26     p compact
# 9          audi a4 quattro   1.8  1999     4   auto(l5)     4    16    25     p compact
# 10         audi a4 quattro   2.0  2008     4 manual(m6)     4    20    28     p compact
# # ... with 224 more rows


# let column 1 (manufacturer) be the x value, 
#     columns 3, 5, 8, and 9 be different y values (displ, cyl, cty, hwy)
# and column 6 be the coloring variable (trans)

mpg_long <-
  tidyr::gather(mpg, key = response, value = value, displ, cyl, cty, hwy)

mpg_long
# # A tibble: 936 × 9
#    manufacturer      model  year      trans   drv    fl   class response value
#           <chr>      <chr> <int>      <chr> <chr> <chr>   <chr>    <chr> <dbl>
# 1          audi         a4  1999   auto(l5)     f     p compact    displ   1.8
# 2          audi         a4  1999 manual(m5)     f     p compact    displ   1.8
# 3          audi         a4  2008 manual(m6)     f     p compact    displ   2.0
# 4          audi         a4  2008   auto(av)     f     p compact    displ   2.0
# 5          audi         a4  1999   auto(l5)     f     p compact    displ   2.8
# 6          audi         a4  1999 manual(m5)     f     p compact    displ   2.8
# 7          audi         a4  2008   auto(av)     f     p compact    displ   3.1
# 8          audi a4 quattro  1999 manual(m5)     4     p compact    displ   1.8
# 9          audi a4 quattro  1999   auto(l5)     4     p compact    displ   1.8
# 10         audi a4 quattro  2008 manual(m6)     4     p compact    displ   2.0
# # ... with 926 more rows


# Graphic
ggplot(mpg_long) +
  aes(x = manufacturer, y = value, color = trans) +
  geom_point() +
  coord_flip()

enter image description here

# Use the shape aesthetic to show which response is which
last_plot() + aes(shape = response)

enter image description here