使用 ggplot2 在 R 上分组条形图和折线图
Grouped bar and line charts on R using ggplot2
我在 Excel 中制作了一个 table,转换为如下所示的数据框:
Sistema DetNP DetProg Indisp Total TotalMax
1 MOTOR EQUIPO MOVIL 744 977 3.54% 1721 977
2 SISTEMA CHASIS 36 873 1.87% 909 873
3 TREN DE POTENCIA 247 527 1.59% 774 527
4 PMs 0 736 1.52% 736 736
5 SUSPENSION Y RUEDAS 100 346 0.92% 446 346
6 SISTEMA HIDRAULICO 118 181 0.62% 299 181
7 SISTEMA ELECTRICO 24V 144 30 0.36% 174 144
8 CABINA 116 20 0.28% 136 116
9 SISTEMA LUBRICACION 24 40 0.13% 64 40
10 SISTEMA DE FRENOS 17 44 0.13% 61 44
据此,如果可能的话,我需要只使用 ggplot 来制作这张图表:
到目前为止,我一直在使用重塑和熔化,但我无法绘制线条:
molten <- melt(dataset, id = c("Sistema","Indisp","Total","TotalMax"))
ggplot(data=molten, aes(x=time, y=Total, fill=variable)) + geom_bar(stat="identity", position=position_dodge()) + geom_line(aes(x=Sistema, y=Indisp),stat="identity")
感谢您的帮助
这至少是一个开始 - 第一个问题是处理两个不同的尺度。 (Two different scale Axis)。编辑:再清理一下。感谢@Joe 的建议。
library(tidyverse)
df <- tibble::tribble(
~Sistema, ~DetNP, ~DetProg, ~Indisp, ~Total, ~TotalMax,
"MOTOR EQUIPO MOVIL", 744, 977, 0.0354, 1721, 977,
"SISTEMA CHASIS", 36, 873, 0.0187, 909, 873,
"TREN DE POTENCIA", 247, 527, 0.0159, 774, 527,
"PMs", 0, 736, 0.0152, 736, 736,
"SUSPENSION Y RUEDAS", 100, 346, 0.0092, 446, 346,
"SISTEMA HIDRAULICO", 118, 181, 0.0062, 299, 181,
"SISTEMA ELECTRICO 24V", 144, 30, 0.0036, 174, 144,
"CABINA", 116, 20, 0.0028, 136, 116,
"SISTEMA LUBRICACION", 24, 40, 0.0013, 64, 40,
"SISTEMA DE FRENOS", 17, 44, 0.0013, 61, 44
)
df
library(ggplot2)
library(reshape2)
library(scales)
molten <- melt(df, id = c("Sistema","Indisp","Total","TotalMax"))
head(molten, 2)
scaleFactor <- max(molten$value) / max(molten$Indisp)
ggplot(data=molten) +
geom_bar(aes( x=reorder(Sistema, -Indisp), y=value, fill=variable),
stat="identity", position=position_dodge()) +
geom_point(aes( x=reorder(Sistema, -Indisp), y=Indisp * scaleFactor, color = "red"),
stat = "identity", show.legend = FALSE) +
geom_line(aes( x=reorder(Sistema, -Indisp), y=Indisp * scaleFactor,
group = variable, color = "red"), stat = "identity", show.legend = FALSE) +
scale_y_continuous(name="HORAS", sec.axis=sec_axis(~./scaleFactor, name="Indisp",
labels = percent_format(accuracy = 0.1))) +
theme_minimal() +
theme(axis.title.y.right = element_text(color = 'red'),
axis.text.y.right = element_text(color = 'red'))+
labs(title = "TOP TEN INDISPONIBLIDAD POR HORAS EN SISTEMAS - JUL 2019")
我在 Excel 中制作了一个 table,转换为如下所示的数据框:
Sistema DetNP DetProg Indisp Total TotalMax
1 MOTOR EQUIPO MOVIL 744 977 3.54% 1721 977
2 SISTEMA CHASIS 36 873 1.87% 909 873
3 TREN DE POTENCIA 247 527 1.59% 774 527
4 PMs 0 736 1.52% 736 736
5 SUSPENSION Y RUEDAS 100 346 0.92% 446 346
6 SISTEMA HIDRAULICO 118 181 0.62% 299 181
7 SISTEMA ELECTRICO 24V 144 30 0.36% 174 144
8 CABINA 116 20 0.28% 136 116
9 SISTEMA LUBRICACION 24 40 0.13% 64 40
10 SISTEMA DE FRENOS 17 44 0.13% 61 44
据此,如果可能的话,我需要只使用 ggplot 来制作这张图表:
到目前为止,我一直在使用重塑和熔化,但我无法绘制线条:
molten <- melt(dataset, id = c("Sistema","Indisp","Total","TotalMax"))
ggplot(data=molten, aes(x=time, y=Total, fill=variable)) + geom_bar(stat="identity", position=position_dodge()) + geom_line(aes(x=Sistema, y=Indisp),stat="identity")
感谢您的帮助
这至少是一个开始 - 第一个问题是处理两个不同的尺度。 (Two different scale Axis)。编辑:再清理一下。感谢@Joe 的建议。
library(tidyverse)
df <- tibble::tribble(
~Sistema, ~DetNP, ~DetProg, ~Indisp, ~Total, ~TotalMax,
"MOTOR EQUIPO MOVIL", 744, 977, 0.0354, 1721, 977,
"SISTEMA CHASIS", 36, 873, 0.0187, 909, 873,
"TREN DE POTENCIA", 247, 527, 0.0159, 774, 527,
"PMs", 0, 736, 0.0152, 736, 736,
"SUSPENSION Y RUEDAS", 100, 346, 0.0092, 446, 346,
"SISTEMA HIDRAULICO", 118, 181, 0.0062, 299, 181,
"SISTEMA ELECTRICO 24V", 144, 30, 0.0036, 174, 144,
"CABINA", 116, 20, 0.0028, 136, 116,
"SISTEMA LUBRICACION", 24, 40, 0.0013, 64, 40,
"SISTEMA DE FRENOS", 17, 44, 0.0013, 61, 44
)
df
library(ggplot2)
library(reshape2)
library(scales)
molten <- melt(df, id = c("Sistema","Indisp","Total","TotalMax"))
head(molten, 2)
scaleFactor <- max(molten$value) / max(molten$Indisp)
ggplot(data=molten) +
geom_bar(aes( x=reorder(Sistema, -Indisp), y=value, fill=variable),
stat="identity", position=position_dodge()) +
geom_point(aes( x=reorder(Sistema, -Indisp), y=Indisp * scaleFactor, color = "red"),
stat = "identity", show.legend = FALSE) +
geom_line(aes( x=reorder(Sistema, -Indisp), y=Indisp * scaleFactor,
group = variable, color = "red"), stat = "identity", show.legend = FALSE) +
scale_y_continuous(name="HORAS", sec.axis=sec_axis(~./scaleFactor, name="Indisp",
labels = percent_format(accuracy = 0.1))) +
theme_minimal() +
theme(axis.title.y.right = element_text(color = 'red'),
axis.text.y.right = element_text(color = 'red'))+
labs(title = "TOP TEN INDISPONIBLIDAD POR HORAS EN SISTEMAS - JUL 2019")