I am not sure if geom_bar
is able (probably I'm not) to create the plot I need with geom_bar
. I want a bar plot but not starting from 0, I have a minimum and maximum value that are to be the start and end points of each bar. Desired output looks like this one:
我不确定geom_bar是否能够(可能我不是)用geom_bar创建我需要的情节。我想要一个条形图,但不是从0开始,我有一个最小值和最大值,它们是每个条形的起点和终点。期望的输出看起来像这样:
Data structure is
数据结构是
dput(datos)
structure(list(CDG = c("Alicante", "Alicante", "Alicante", "Alicante",
"Alicante", "Alicante", "Alicante", "Alicante", "Alicante", "Alicante",
"Alicante", "Castellón", "Castellón", "Castellón", "Castellón",
"Castellón", "Castellón", "Castellón", "Castellón", "Castellón",
"Castellón", "Castellón", "Castellón", "Castellón", "Castellón",
"Castellón", "Castellón", "Castellón", "Castellón", "Valencia",
"Valencia", "Valencia", "Valencia", "Valencia", "Valencia", "Valencia",
"Valencia", "Valencia", "Valencia", "Valencia", "Valencia", "Valencia",
"Valencia", "Valencia", "Valencia", "Valencia", "Valencia", "Valencia",
"Valencia", "Valencia", "Valencia", "Valencia", "Valencia", "Valencia",
"Valencia"), ESTACION = structure(c(5L, 1L, 2L, 3L, 8L, 23L,
24L, 21L, 31L, 22L, 41L, 26L, 12L, 16L, 13L, 14L, 15L, 18L, 28L,
29L, 19L, 37L, 39L, 27L, 49L, 52L, 53L, 54L, 55L, 4L, 7L, 6L,
9L, 10L, 11L, 17L, 20L, 33L, 25L, 30L, 32L, 36L, 35L, 34L, 38L,
42L, 46L, 48L, 47L, 43L, 45L, 44L, 50L, 51L, 40L), .Label = c("Alacant-El_Pla",
"Alacant-Florida_Babel", "Alacant-Rabassa", "Albalat_dels_Tarongers",
"Alcoi-Verge_dels_Lliris", "Algar_de_Palancia", "Alzira", "Benidorm",
"Beniganim", "Bunnol-Cemex", "Burjassot-Facultats", "Burriana",
"Castello-Ermita", "Castello-Grau", "Castello-Patronat_d.Esports",
"Castello-Penyeta", "Caudete_de_las_Fuentes", "Cirat", "Coratxar",
"Cortes_de_Pall", "Elda-Lacy", "El_Pinos", "Elx-Agroalimentari",
"Elx-Parc_de_Bombers", "Gandia", "L.Alcora", "La_Vall_d.Uix",
"Morella", "Onda", "Ontinyent", "Orihuela", "Paterna-CEAM", "Quart_de_Poblet",
"Sagunt-CEA", "Sagunt-Nord", "Sagunt-Port", "Sant_Jordi", "Torrebaja",
"Torre_Endomenech", "Torrent-El_Vedat", "Torrevieja", "Valencia-Albufera",
"Valencia-Avd._Francia", "Valencia-Bulevard_Sud", "Valencia-Moli_del_Sol",
"Valencia-Pista_de_Silla", "Valencia-Politecnic", "Valencia-Vivers",
"Vilafranca", "Vilamarxant", "Villar_del_Arzobispo", "Vinaros_Planta",
"Vinaros_Plataforma", "Viver", "Zorita"), class = "factor"),
MAXIMO_HORARIO = c(NA, 70.7, 80, 63.7, 93.5, 73.8, 82.3,
59, 71, 85.5, 62, 73, 38, 87.2, 89.2, 82, 28.5, 84, 99.5,
86, 87, 94, 84, 70, 85.5, 91.2, 85.3, 72.4, 97.5, NA, 63.3,
96.2, 81, 68, 72.3, 64.5, 67.4, 59.5, 82.7, 77, 65.5, 89.5,
76.5, NA, 80, NA, 65.3, 66.2, 72.5, 65.8, 50.2, 54.8, 71.2,
79.5, NA), PROMEDIO_DIARIO = c(NA, 56.8, 64.5, 52.9, 86.9,
69.6, 79.4, 59, 73.9, 82.4, 61.4, 62.2, 43.1, 85.1, 55.2,
77.7, 26.2, 81.9, 94.3, 85.8, 85.1, 89.8, 80.5, 66.6, 83.3,
94.3, 87.5, 72.9, 93.3, 100.3, 60.8, 94.2, 74, 68.4, 62.5,
66.7, 66.5, 45.5, 78, 79.4, 68.2, 81.1, 72.6, NA, 77.1, NA,
55.1, 54.7, 66.8, 54.1, 49.3, 46.6, 63.9, 81.8, NA), MINIMO_HORARIO = c(NA,
26.8, 24.5, 30.2, 62, 46.5, 56.2, 59, 58.2, 67.8, 50.8, 16.7,
11.5, 63.2, 2, 62, 18, 75.2, 87.5, 80, 77.8, 83.2, 71.8,
52.5, 78.3, 88.9, 83.7, 66.6, 84.3, NA, 26.2, 80, 42, 47.2,
8.2, 43, 45.5, 4.5, 47.5, 55.7, 39.8, 68.7, 58.8, NA, 74.3,
NA, 11.5, 7.2, 25.5, 18.2, 8.5, 7.2, 14.7, 70.5, NA), MAXIMO_OCTOHORARIO = c(NA,
66.1, 81.1, 55.2, 96.9, 79.2, 89.1, NA, 81.1, 90.5, 64.6,
79.8, 66.1, 91.4, 80.8, 80.8, 28.4, 83.8, 96.1, 88.3, 88.5,
92.3, 82.7, 71.4, 85.6, 97.4, 89.1, 75.5, 95.1, NA, 79.5,
100.3, 90.4, 76.1, 83.3, 75.5, 79.7, 66.7, 87.7, 87.5, 78.3,
83.8, 76.8, NA, 76.8, NA, 74.5, 73, 81.2, 67.8, 73.1, 69,
79.9, 88.2, NA), VARIACION_MAX_HOR = c(NA, -15.1, -7, -21.3,
-10.2, -17.2, -18, NA, -18.2, -12.8, -8.2, -23.8, -57.8,
-8, 2.7, -1.3, -0.8, -5.7, -1.5, -10.8, -5.8, -0.8, -8.3,
-19.7, -6.5, -7.7, -11.9, -16.8, -2.7, NA, -27.7, -19.1,
-19.5, -24, -21.7, -28.2, -22.6, -23.7, -17.8, -25, -29.2,
-5.2, -19.5, NA, -14.3, NA, -32.2, -20.5, -20.7, -20.4, -38.1,
-29, -21.6, -20.5, NA), VARIACION_PRM_DIA = c(NA, -16.2,
-10.4, -18.9, -4, -5.8, -7.7, NA, -3, -9.4, 2, -22.9, -27.2,
1.1, -4.7, 16.8, -0.9, 0, 0.5, -1.9, 1.3, 15.1, 23.7, -14.1,
-4.8, 1.7, 14.8, -8, 14.7, -0.7, -20, -7.6, -17.9, -12.7,
-20.7, -10.7, -14.7, -24.3, -9.1, -13.6, -11.8, 18, -10.1,
NA, 10.9, NA, -24.4, -20.2, -14.9, -18.7, -25.4, -30, -18.6,
-6.7, NA), OSCILACION_DIARIO = c(NA, 43.9, 55.5, 33.5, 31.5,
27.3, 26.1, 0, 12.8, 17.7, 11.2, 56.3, 26.5, 24, 87.2, 20,
10.5, 8.8, 12, 6, 9.2, 10.8, 12.2, 17.5, 7.2, 2.3, 1.6, 5.8,
13.2, NA, 37.1, 16.2, 39, 20.8, 64.1, 21.5, 21.9, 55, 35.2,
21.3, 25.7, 20.8, 17.7, NA, 5.7, NA, 53.8, 59, 47, 47.6,
41.7, 47.6, 56.5, 9, NA), ESTACIONALIDAD_MAX = c(NA, -43,
-37.1, -50.2, -30.3, -46.8, -35.7, -55.6, -44.6, -36.1, -48.1,
-45.6, -72.3, -37.5, -29.7, -38.8, -75.3, -38.9, -29.1, -39.4,
-36.9, -30.3, -33.6, -42, -40.7, -17.3, -33.4, -46.7, -28.9,
NA, -53.2, -22, -41.9, -46.8, -43, -51, -43.6, -50.1, -39.4,
-43.3, -45.7, -32.5, -39.8, NA, -35.2, NA, -39.5, -43.3,
-39.9, -43.3, -58.1, -50.9, -45, -44.3, NA), ESTACIONALIDAD_MED = c(NA,
-15.2, -10.7, -28.9, -4.3, -11, 3.8, -17.6, 20.2, -4.2, -30.9,
-10.4, -27.6, 2.7, -0.4, 17.5, -61.5, 4.7, -5.7, 6.1, -12.7,
6.9, 18.7, -10.2, -9.1, 30.4, -12, -3.3, 20.2, 32.1, -5.1,
22, 2.4, -2.8, -6.9, -18.7, -13.9, -16.2, 10.6, -4.1, 1.5,
22.1, 12.9, NA, 12.2, NA, 12.9, -3.7, -1.6, -13.3, -19.3,
-25, -15.5, -0.8, NA), X = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA)), .Names = c("CDG", "ESTACION", "MAXIMO_HORARIO",
"PROMEDIO_DIARIO", "MINIMO_HORARIO", "MAXIMO_OCTOHORARIO", "VARIACION_MAX_HOR",
"VARIACION_PRM_DIA", "OSCILACION_DIARIO", "ESTACIONALIDAD_MAX",
"ESTACIONALIDAD_MED", "X"), row.names = c(NA, -55L), class = "data.frame")
R script used to plot other variables in the dataset follows
用于绘制数据集中其他变量的R脚本如下
library(ggplot2)
library(reshape2)
library(grid)
datos=read.csv("previozono.csv",header=T,sep=";", na.strings="-99.9")
datap1=cbind.data.frame(datos$ESTACION,datos$MAXIMO_HORARIO,datos$PROMEDIO_DIARIO)
names(datap1) <- c("estacion","Maximo_horario","Promedio_diario")
dataplot1 <- melt(datap1,id.vars='estacion')
plot1=ggplot(dataplot1)+ geom_bar(aes(x=estacion,y=value,fill=variable),
stat='identity',position='dodge') + xlab(" ") + ylab(" ") +
theme(axis.text.x=element_text(angle=80, size=8, hjust=1, vjust=1),legend.position="bottom") +
theme(axis.text.y=element_text(size=8)) +
scale_y_continuous(limits = c(0,250),breaks=c(0,50,100,150,200,250)) +
theme(plot.margin=unit(c(0,1,0,0),"mm")) +
ggtitle(paste("Ozono - Máximo horario y promedio diario - ",format(Sys.time(), "%d/%m/%Y"),sep="")) +
scale_fill_manual(values=c("#E95875", "#99CCFF")) + labs(fill="")
2 个解决方案
#1
8
You can achieve this with geom_segment
like this (you will need the wide format data of datap1
for this solution):
你可以用这样的geom_segment来实现这个目的(你需要这个解决方案的datap1的宽格式数据):
ggplot(datap1) +
geom_segment(aes(x=estacion, xend=estacion, y=Promedio_diario, yend=Maximo_horario), size=3, color="orange") +
geom_hline(yintercept=mean((datap1$Maximo_horario + datap1$Promedio_diario)/2, na.rm=TRUE), color="red") +
theme_bw() +
theme(axis.text.x=element_text(angle=90))
which gives:
这使:
Using geom_linerange
will give you the same result:
使用geom_linerange会得到相同的结果:
ggplot(datap1) +
geom_linerange(aes(x=estacion, y=average, ymin=Promedio_diario, ymax=Maximo_horario), size=3, color="orange") +
geom_hline(yintercept=mean((datap1$Maximo_horario + datap1$Promedio_diario)/2, na.rm=TRUE), color="red") +
theme_bw() +
theme(axis.text.x=element_text(angle=90))
And if you want to include the mean for each bar, geom_pointrange
might be an option as well.
如果你想包括每个条形的均值,geom_pointrange也可能是一个选项。
#2
7
Here is the start:
这是一个开始:
ggplot(dataplot1,aes(x=estacion,y=value,fill=variable)) +
geom_bar(stat='identity') +
scale_fill_manual(values=c("white", "#99CCFF")) +
theme_classic()
Stack them then plot one group as background colour - white:
堆叠它们然后将一组绘制为背景颜色 - 白色:
#1
8
You can achieve this with geom_segment
like this (you will need the wide format data of datap1
for this solution):
你可以用这样的geom_segment来实现这个目的(你需要这个解决方案的datap1的宽格式数据):
ggplot(datap1) +
geom_segment(aes(x=estacion, xend=estacion, y=Promedio_diario, yend=Maximo_horario), size=3, color="orange") +
geom_hline(yintercept=mean((datap1$Maximo_horario + datap1$Promedio_diario)/2, na.rm=TRUE), color="red") +
theme_bw() +
theme(axis.text.x=element_text(angle=90))
which gives:
这使:
Using geom_linerange
will give you the same result:
使用geom_linerange会得到相同的结果:
ggplot(datap1) +
geom_linerange(aes(x=estacion, y=average, ymin=Promedio_diario, ymax=Maximo_horario), size=3, color="orange") +
geom_hline(yintercept=mean((datap1$Maximo_horario + datap1$Promedio_diario)/2, na.rm=TRUE), color="red") +
theme_bw() +
theme(axis.text.x=element_text(angle=90))
And if you want to include the mean for each bar, geom_pointrange
might be an option as well.
如果你想包括每个条形的均值,geom_pointrange也可能是一个选项。
#2
7
Here is the start:
这是一个开始:
ggplot(dataplot1,aes(x=estacion,y=value,fill=variable)) +
geom_bar(stat='identity') +
scale_fill_manual(values=c("white", "#99CCFF")) +
theme_classic()
Stack them then plot one group as background colour - white:
堆叠它们然后将一组绘制为背景颜色 - 白色: