简体   繁体   English

如何将列用作x轴

[英]How to use the columns as x-axis

I have a hard time to create an actually easy plot. 我很难创建一个简单的图。 I prepared some data as an example I want to plot: 我准备了一些数据作为要绘制的示例:

m.alphabet_summary <-
structure(c(194L, 185L, 208L, 219L, 194L, 161L, 205L, 219L, 188L, 
227L, 210L, 189L, 196L, 213L, 192L, 207L, 220L, 203L, 181L, 189L, 
221L, 192L, 189L, 205L, 193L, 188L, 187L, 209L, 189L, 227L, 213L, 
200L, 206L, 194L, 187L, 184L, 209L, 223L, 198L, 186L, 183L, 232L, 
191L, 190L, 204L, 209L, 178L, 203L, 184L, 226L), .Dim = c(5L, 
10L), .Dimnames = list(c("As", "Bs", "Cs", "Ds", "Os"), NULL))

#   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#As  194  161  210  207  221  188  213  184  183   209
#Bs  185  205  189  220  192  187  200  209  232   178
#Cs  208  219  196  203  189  209  206  223  191   203
#Ds  219  188  213  181  205  189  194  198  190   184
#Os  194  227  192  189  193  227  187  186  204   226

I would like to have the Y-axis as the values within the cells (values ranging around 200) and the x axis should be the number of the column (ranging 1-10). 我想将Y轴作为单元格中的值(范围在200左右的值),而x轴应作为列号(范围为1-10)。 the grouping/color should be according to the column names (a,b,c,d,o) 分组/颜色应根据列名(a,b,c,d,o)

thank you so much for any advice! 非常感谢您的任何建议!

I would use ggplot, and skip most of your data processing - ggplot will take care of that. 我将使用ggplot,并跳过大部分数据处理-ggplot会解决这一问题。

sampling <- c("o", "b", "c", "d", "a")
alphabet <- sample(sampling, 10000, replace = TRUE)
group <- factor(rep(1:10, each = 1000))
df <- data.frame(group, alphabet)

library(ggplot2)
ggplot(df, aes(x = group, fill = alphabet)) + 
  geom_bar(position = "dodge")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM