用数据表绘制堆积条形图

时间:2015-09-23 03:32:45

标签: r ggplot2 data.table

我正在尝试绘制一个堆积的条形图,但我遗漏了一些东西。我有以下数据表示例:

       hdisk86 hdisk88 hdisk90 hdisk89 hdisk91 hdisk92 hdisk93 hdisk94 hdisk96
1:       0       0       0       0       0       0       0       0       0
2:       0       0       0       0       0       0       0       0       0
3:       0       0       0       0       0       0       0       0       0
   hdisk95 hdisk98 hdisk97 hdisk99 hdisk100 hdisk101 hdisk102 hdisk103 hdisk108
1:       0       0       0       0        0        0        0        0        0
2:       0       0       0       0        0        0        0        0        0
3:       0       0       0       0        0        0        0        0        0
   hdisk107 hdisk104 hdisk105 hdisk109 hdisk110 hdisk112 hdisk111 hdisk113
1:        0        0        0        0        0        0        0        0
2:        0        0        0        0        0        0        0        0
3:        0        0        0        0        0        0        0        0
   hdisk114 hdisk115 hdisk116 hdisk117 hdisk87 hdisk118 hdisk120 hdisk119
1:        0        0        0        0       0        0        0        0
2:        0        0        0        0       0        0        0        0
3:        0        0        0        0       0        0        0        0
   hdisk122 hdisk123 hdisk124 hdisk125 hdisk121 hdisk127 hdisk126 hdisk2 hdisk3
1:        0        0        0        0        0        0        0      0      0
2:        0        0        0        0        0        0        0      0      0
3:        0        0        0        0        0        0        0      0      0
   hdisk5 hdisk4 hdisk6 hdisk10 hdisk11 hdisk8 hdisk12 hdisk9 hdisk18 hdisk14
1:      0      0      0       0       0      0       0      0       0       0
2:      0      0      0       0       0      0       0      0       0       0
3:      0      0      0       0       0      0       0      0       0       0
   hdisk15 hdisk17 hdisk16 hdisk13 hdisk106 hdisk19 hdisk20 hdisk7 hdisk21
1:       0       0       0       0        0       0       0      0       0
2:       0       0       0       0        0       0       0      0       0
3:       0       0       0       0        0       0       0      0       0
   hdisk28 hdisk33 hdisk32 hdisk27 hdisk30 hdisk23 hdisk35 hdisk40 hdisk25
1:       0       0       0       0       0       0       0       0       0
2:       0       0       0       0       0       0       0       0       0
3:       0       0       0       0       0       0       0       0       0
   hdisk41 hdisk39 hdisk38 hdisk43 hdisk22 hdisk36 hdisk31 hdisk45 hdisk29
1:       0       0       0       0       0       0       0       0       0
2:       0       0       0       0       0       0       0       0       0
3:       0       0       0       0       0       0       0       0       0
   hdisk44 hdisk34 hdisk37 hdisk48 hdisk24 hdisk47 hdisk42 hdisk46 hdisk49
1:       0       0       0       0       0       0       0       0       0
2:       0       0       0       0       0       0       0       0       0
3:       0       0       0       0       0       0       0       0       0
   hdisk53 hdisk50 hdisk56 hdisk55 hdisk54 hdisk52 hdisk59 hdisk62 hdisk58
1:       0       0       0       0       0       0       0       0       0
2:       0       0       0       0       0       0       0       0       0
3:       0       0       0       0       0       0       0       0       0
   hdisk64 hdisk61 hdisk65 hdisk60 hdisk67 hdisk66 hdisk57 hdisk51 hdisk69
1:       0       0       0       0       0       0       0       0       0
2:       0       0       0       0       0       0       0       0       0
3:       0       0       0       0       0       0       0       0       0
   hdisk63 hdisk74 hdisk70 hdisk72 hdisk75 hdisk68 hdisk73 hdisk76 hdisk71
1:       0       0       0       0       0       0       0       0       0
2:       0       0       0       0       0       0       0       0       0
3:       0       0       0       0       0       0       0       0       0
   hdisk78 hdisk85 hdisk81 hdisk80 hdisk83 hdisk79 hdisk82 hdisk77 hdisk84
1:       0       0       0       0       0       0       0       0       0
2:       0       0       0       0       0       0       0       0       0
3:       0       0       0       0       0       0       0       0       0
   hdisk26 hdisk0 hdisk1 hdisk128 hdisk129 hdisk130 hdisk131 hdisk132 hdisk133
1:       0      0      0        0        0        0        0        0        0
2:       0      0      0        0        0        0        0        0        0
3:       0      0      0        0        0        0        0        0        0
   hdisk134 hdisk135 hdisk136 hdisk137 hdisk138 hdisk139 hdisk140 hdisk141
1:        0        0        0        0        0        0        0        0
2:        0        0        0        0        0        0        0        0
3:        0        0        0        0        0        0        0        0
   hdisk142 hdisk143 hdisk144 hdisk145 hdisk146 hdisk147 hdisk148 hdisk149
1:        0        0        0        0        0        0        0        0
2:        0        0        0        0        0        0        0        0
3:        0        0        0        0        0        0        0        0
   Summary
1:     Avg
2:    WAvg
3:     Max

我使用了熔化函数将数据表从宽转换为长:

d <- grep("hdisk", names(DT), value = T)
DT_mdf <- melt(DT, 
                id.vars="Summary", 
                measure.vars=d,
                variable.name="hdisks",
                value.name="percentage")

得到了这个:

     Summary   hdisks percentage
  1:     Avg  hdisk86          0
  2:    WAvg  hdisk86          0
  3:     Max  hdisk86          0
  4:     Avg  hdisk88          0
  5:    WAvg  hdisk88          0
 ---                            
446:    WAvg hdisk148          0
447:     Max hdisk148          0
448:     Avg hdisk149          0
449:    WAvg hdisk149          0
450:     Max hdisk149          0

然后我尝试绘制堆积的条形图并保存到png文件:

# Plot line graphics...
g1 <- ggplot(DT_mdf, aes(x=hdisks, y=percentage, fill=Summary)) +
    geom_bar(stat = "identity") +
    theme_excel() +
    scale_color_excel() +
    scale_fill_excel()

# Saving in png format
png("CPU_ALL_g1.png", width=16*ppi, height=9*ppi, res=ppi)
plot(g1)
dev.off()

我收到以下错误消息:

Saving 7 x 7 in image
Don't know how to automatically pick scale for object of type list. Defaulting to continuous
Error in pmin(y, 0) : object 'y' not found
Calls: ggsave ... transform -> transform.data.frame -> eval -> eval -> pmin
Execution halted

我是否有太多元素可以在单个图表中绘图?或其他什么?

这里是长格式的数据表样本:
structure(list(Summary = list("Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max", "Avg", "WAvg", "Max"), hdisks = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L, 20L, 20L, 20L, 21L, 21L, 21L, 22L, 22L, 22L, 23L, 23L, 23L, 24L, 24L, 24L, 25L, 25L, 25L, 26L, 26L, 26L, 27L, 27L, 27L, 28L, 28L, 28L, 29L, 29L, 29L, 30L, 30L, 30L, 31L, 31L, 31L, 32L, 32L, 32L, 33L, 33L, 33L, 34L, 34L, 34L, 35L, 35L, 35L, 36L, 36L, 36L, 37L, 37L, 37L, 38L, 38L, 38L, 39L, 39L, 39L, 40L, 40L, 40L, 41L, 41L, 41L, 42L, 42L, 42L, 43L, 43L, 43L, 44L, 44L, 44L, 45L, 45L, 45L, 46L, 46L, 46L, 47L, 47L, 47L, 48L, 48L, 48L, 49L, 49L, 49L, 50L, 50L, 50L, 51L, 51L, 51L, 52L, 52L, 52L, 53L, 53L, 53L, 54L, 54L, 54L, 55L, 55L, 55L, 56L, 56L, 56L, 57L, 57L, 57L, 58L, 58L, 58L, 59L, 59L, 59L, 60L, 60L, 60L, 61L, 61L, 61L, 62L, 62L, 62L, 63L, 63L, 63L, 64L, 64L, 64L, 65L, 65L, 65L, 66L, 66L, 66L, 67L, 67L, 67L, 68L, 68L, 68L, 69L, 69L, 69L, 70L, 70L, 70L, 71L, 71L, 71L, 72L, 72L, 72L, 73L, 73L, 73L, 74L, 74L, 74L, 75L, 75L, 75L, 76L, 76L, 76L, 77L, 77L, 77L, 78L, 78L, 78L, 79L, 79L, 79L, 80L, 80L, 80L, 81L, 81L, 81L, 82L, 82L, 82L, 83L, 83L, 83L, 84L, 84L, 84L, 85L, 85L, 85L, 86L, 86L, 86L, 87L, 87L, 87L, 88L, 88L, 88L, 89L, 89L, 89L, 90L, 90L, 90L, 91L, 91L, 91L, 92L, 92L, 92L, 93L, 93L, 93L, 94L, 94L, 94L, 95L, 95L, 95L, 96L, 96L, 96L, 97L, 97L, 97L, 98L, 98L, 98L, 99L, 99L, 99L, 100L, 100L, 100L, 101L, 101L, 101L, 102L, 102L, 102L, 103L, 103L, 103L, 104L, 104L, 104L, 105L, 105L, 105L, 106L, 106L, 106L, 107L, 107L, 107L, 108L, 108L, 108L, 109L, 109L, 109L, 110L, 110L, 110L, 111L, 111L, 111L, 112L, 112L, 112L, 113L, 113L, 113L, 114L, 114L, 114L, 115L, 115L, 115L, 116L, 116L, 116L, 117L, 117L, 117L, 118L, 118L, 118L, 119L, 119L, 119L, 120L, 120L, 120L, 121L, 121L, 121L, 122L, 122L, 122L, 123L, 123L, 123L, 124L, 124L, 124L, 125L, 125L, 125L, 126L, 126L, 126L, 127L, 127L, 127L, 128L, 128L, 128L, 129L, 129L, 129L, 130L, 130L, 130L, 131L, 131L, 131L, 132L, 132L, 132L, 133L, 133L, 133L, 134L, 134L, 134L, 135L, 135L, 135L, 136L, 136L, 136L, 137L, 137L, 137L, 138L, 138L, 138L, 139L, 139L, 139L, 140L, 140L, 140L, 141L, 141L, 141L, 142L, 142L, 142L, 143L, 143L, 143L, 144L, 144L, 144L, 145L, 145L, 145L, 146L, 146L, 146L, 147L, 147L, 147L, 148L, 148L, 148L, 149L, 149L, 149L, 150L, 150L, 150L), class = "factor", .Label = c("hdisk86", "hdisk88", "hdisk90", "hdisk89", "hdisk91", "hdisk92", "hdisk93", "hdisk94", "hdisk96", "hdisk95", "hdisk98", "hdisk97", "hdisk99", "hdisk100", "hdisk101", "hdisk102", "hdisk103", "hdisk108", "hdisk107", "hdisk104", "hdisk105", "hdisk109", "hdisk110", "hdisk112", "hdisk111", "hdisk113", "hdisk114", "hdisk115", "hdisk116", "hdisk117", "hdisk87", "hdisk118", "hdisk120", "hdisk119", "hdisk122", "hdisk123", "hdisk124", "hdisk125", "hdisk121", "hdisk127", "hdisk126", "hdisk2", "hdisk3", "hdisk5", "hdisk4", "hdisk6", "hdisk10", "hdisk11", "hdisk8", "hdisk12", "hdisk9", "hdisk18", "hdisk14", "hdisk15", "hdisk17", "hdisk16", "hdisk13", "hdisk106", "hdisk19", "hdisk20", "hdisk7", "hdisk21", "hdisk28", "hdisk33", "hdisk32", "hdisk27", "hdisk30", "hdisk23", "hdisk35", "hdisk40", "hdisk25", "hdisk41", "hdisk39", "hdisk38", "hdisk43", "hdisk22", "hdisk36", "hdisk31", "hdisk45", "hdisk29", "hdisk44", "hdisk34", "hdisk37", "hdisk48", "hdisk24", "hdisk47", "hdisk42", "hdisk46", "hdisk49", "hdisk53", "hdisk50", "hdisk56", "hdisk55", "hdisk54", "hdisk52", "hdisk59", "hdisk62", "hdisk58", "hdisk64", "hdisk61", "hdisk65", "hdisk60", "hdisk67", "hdisk66", "hdisk57", "hdisk51", "hdisk69", "hdisk63", "hdisk74", "hdisk70", "hdisk72", "hdisk75", "hdisk68", "hdisk73", "hdisk76", "hdisk71", "hdisk78", "hdisk85", "hdisk81", "hdisk80", "hdisk83", "hdisk79", "hdisk82", "hdisk77", "hdisk84", "hdisk26", "hdisk0", "hdisk1", "hdisk128", "hdisk129", "hdisk130", "hdisk131", "hdisk132", "hdisk133", "hdisk134", "hdisk135", "hdisk136", "hdisk137", "hdisk138", "hdisk139", "hdisk140", "hdisk141", "hdisk142", "hdisk143", "hdisk144", "hdisk145", "hdisk146", "hdisk147", "hdisk148", "hdisk149")), percentage = list(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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= c(NA, -450L), class = c("data.table", "data.frame" ), .internal.selfref = <pointer: 0x1d47ec8>)

1 个答案:

答案 0 :(得分:1)

res替换为units

尝试使用png("CPU_ALL_g1.png", width=16, height=9, units="px")