繁体   English   中英

如何绘制条形图?

[英]How to draw a barplot?

我需要构建一个条形图 plot ,它将在资源列中显示访问过guard1、guard2 和guard 3 的实体(“名称”)的数量。在此处输入图像描述

这是一个解决方案。 请注意,我为您的数据构建示例的第一部分应该在您的问题中,以帮助我们回答。 提供你尝试过的东西也是一个好的开始。

# here is a random copy from your data.frame format
df <- data.frame(name = sample(c(paste0("car", 1:8)), 20, replace = TRUE), 
                 resource = sample(c("guard1", "guard2", "guard3", "electronicSeller"),
                                   20, replace = TRUE))

# Extract unique names for table with only rows for one guard number
g1 <- unique( df$name[ df$resource == "guard1" ] )
g2 <- unique( df$name[ df$resource == "guard2" ] )
g3 <- unique( df$name[ df$resource == "guard3" ] )

# barplot the length of each vector. 
barplot( c(length(g1), length(g2), length(g3) ), 
        names.arg = c("guard1", "guard2", "guard3"))

我相信您需要一个条形图,@Gowachin 回答的解决方案最适合您。

如果您需要直方图,可能会检查您的数据分布,我使用以下代码:

hist(df$variable, 
     main="Title", 
     xlab="unit on x axis", 
     border="black", 
     col="lightskyblue1",
     xlim=c(min(df$variable),max(df$variable)),
     las=1, 
     breaks=35)

暂无
暂无

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

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