r - 如何创建具有多个分组条形的图形?
问题描述
我正在尝试使用ggplot2
多个分组条创建图表。在我的数据集中,我需要选择三列,以便根据我制作的草图对该图表进行分组:
代码:
library(dplyr)
library(tidyr)
library(ggplot2)
DOPTransformationsMean %>%
select(SPL, AddedFields, ModifiedFields, RemovedFields) %>%
gather(Var,Val,-SPL) %>%
ggplot(aes(SPL, Val, group = Var)) +
ylab("Quantity") +
geom_bar(stat="identity", width = 0.3) +
scale_fill_manual(values=c("solid", "solid", "solid")) +
scale_color_manual(labels = c("Added Fields", "Modified Fields", "Removed Fields"), values=c('#b30000','#00b300','#00b3b3')) +
theme_bw(base_size = 30) +
theme(plot.title = element_text(hjust = 0.5), legend.title=element_blank(),legend.position = "bottom", legend.text=element_text(size=27), legend.direction="vertical") +
scale_y_continuous(breaks = function(x) unique(floor(pretty(seq(0, (max(x) + 1) * 1.1)))))
代码结果:
数据集:
SPL,AddedClasses,ModifiedClasses,RemovedClasses,AddedMethods,ModifiedMethods,RemovedMethods,AddedImports,RemovedImports,AddedFields,ModifiedFields,RemovedFields
Reminder-PL,49,76,0,99,78,1,43,0,62,0,2
Iris-PL,84,21,4,14,8,0,34,0,2,0,0
我遵循了一个类似的示例,可以生成折线图,但是通过对我的数据集进行分组,我无法对条形图进行分组。
解决方案
您必须告诉ggplot
根据aes
情节部分中的组填充条形图。
DOPTransformationsMean %>%
select(SPL, AddedFields, ModifiedFields, RemovedFields) %>%
gather(Var,Val,-SPL) %>%
ggplot(aes(SPL, Val, group = Var, fill = Var)) +
ylab("Quantity") +
geom_bar(stat="identity", width = 0.3, position = "dodge") +
scale_fill_manual(labels = c("Added Fields", "Modified Fields", "Removed Fields"),
values=c('#b30000','#00b300','#00b3b3')) +
theme_bw(base_size = 30) +
theme(plot.title = element_text(hjust = 0.5), legend.title=element_blank(),
legend.position = "bottom", legend.text=element_text(size=27),
legend.direction="vertical") +
scale_y_continuous(breaks = function(x) unique(floor(pretty(seq(0, (max(x) + 1) * 1.1)))))
推荐阅读
- swift - 一个集合不相等,但是这个集合中的一个数组是相等的?
- mysql - 我不知道如何使用 angular7 删除值
- ruby-on-rails - 从 Rails 视图中删除逻辑
- python - 在 matplotlib 中的 x 轴和 y 轴位置上绘制值时面临问题
- macos - 防止 IntelliJ IDEA 生成报告分配失败 - JavaScript 堆内存不足
- angular - 以 ng-content 作为正文重复 ng-template(创建 ng-content 的副本)
- java - 如何在java中的JTextField中接受输入后添加异常处理
- apache-kafka - Spring Kafka中的Exactly-once语义
- ftp - 从具有服务器限制的纯 ftp 服务器下载所有文件
- uml - 如何在活动图中为循环使用扩展区域?