Puedes seguir el tutorial por vídeo en https://youtu.be/Peauuvz6v9U
setwd("C:/Users/Raul Ortiz/Documents/Expression/Expression Encoder/Output/41 GGPLOT2. Anadir textos al grafico y a las columnas I")
Ya usamos el mismo en vídeos anteriores
Tratamiento = factor(c(1,2,1,2,1,2),labels=c("Testigo", "Tratado"))
Color = factor(c(1,1,2,2,3,3),labels=c("Verde","Envero","Negra"))
Aceitunas = c(96,30,18,10,20,108)
df=data.frame(Tratamiento,Color,Aceitunas)
df
## Tratamiento Color Aceitunas
## 1 Testigo Verde 96
## 2 Tratado Verde 30
## 3 Testigo Envero 18
## 4 Tratado Envero 10
## 5 Testigo Negra 20
## 6 Tratado Negra 108
TemaTitulo = element_text(family="Comic Sans MS",
size=rel(2), #Tamaño relativo de la letra del título
vjust=2, #Justificación vertical, para separarlo del Gráfico
face="bold", #Letra negrilla. Otras posibilidades "plain", "italic", "bold" y "bold.italic"
color="red", #Color del texto
lineheight=1.5) #Separación entre líneas
Grafico = ggplot(data=df, aes(x=Tratamiento, y=Aceitunas, fill=Color)) +
geom_bar(stat="identity", position=position_dodge()) +
scale_fill_manual(values=c("darkgreen", "purple","black")) +
theme (text = element_text(size=8)) + # Tamaño de fuente del grafico por defecto
ggtitle ("Estado de maduracion de aceitunas \n con distintos tratamientos") + # título del Gráfico
theme (plot.title = TemaTitulo) + # Etiquetas o títulos de los ejes
theme (axis.title = element_text(face="bold", colour="blue", size=rel(1.5))) # Características de los títulos de los ejes
Grafico
Grafico + geom_text(data = NULL, x = 1.5, y = 90, label = "Texto flotante1")
Grafico + geom_text(aes(y = Aceitunas, ymax = Aceitunas, label = Aceitunas),
position = position_dodge(width = 0.9), size=3, vjust=-1, hjust=0.5 ,col="black")
El texto, al igual que en el Gráfico anterior, se coloca en la parte superior de cada tramo.
ggplot(data=df, aes(x=Tratamiento, y=Aceitunas, fill=Color)) +
geom_bar(stat="identity", position=position_stack()) +
geom_text(aes(y = Aceitunas, ymax = Aceitunas, label = Aceitunas),
position = position_stack(), size=3, vjust=2, hjust=0.5 ,col="white") +
scale_fill_manual(values=c("darkgreen", "purple","black")) +
theme (text = element_text(size=8)) +
ggtitle ("Estado de maduracion de aceitunas \n con distintos tratamientos") +
theme (plot.title = TemaTitulo) +
theme (axis.title = element_text(face="bold", colour="blue", size=rel(1.5)))
library(plyr)
#Mediante el paquete "plyr".
df <- ddply(df, "Tratamiento", transform, PosEtiq = cumsum(Aceitunas)-0.5*Aceitunas)
df
## Tratamiento Color Aceitunas PosEtiq
## 1 Testigo Verde 96 48
## 2 Testigo Envero 18 105
## 3 Testigo Negra 20 124
## 4 Tratado Verde 30 15
## 5 Tratado Envero 10 35
## 6 Tratado Negra 108 94
ggplot(data=df, aes(x=Tratamiento, y=Aceitunas, fill=Color)) +
geom_bar(stat="identity", position=position_stack()) +
geom_text(aes(y = PosEtiq, label = Aceitunas), size=3, col="white") +
scale_fill_manual(values=c("darkgreen", "purple","black")) +
theme (text = element_text(size=14)) +
ggtitle ("Estado de maduracion de aceitunas \n con distintos tratamientos") +
theme (plot.title = TemaTitulo) +
theme (axis.title = element_text(face="bold", colour="blue", size=rel(1.5)))
df <- ddply(df, "Tratamiento", transform,
PorcentajeAce = Aceitunas / sum(Aceitunas) * 100)
df <- ddply(df, "Tratamiento", transform,
Etiquetas = paste(round(PorcentajeAce, digits=0), '%'))
df
## Tratamiento Color Aceitunas PosEtiq PorcentajeAce Etiquetas
## 1 Testigo Verde 96 48 71.641791 72 %
## 2 Testigo Envero 18 105 13.432836 13 %
## 3 Testigo Negra 20 124 14.925373 15 %
## 4 Tratado Verde 30 15 20.270270 20 %
## 5 Tratado Envero 10 35 6.756757 7 %
## 6 Tratado Negra 108 94 72.972973 73 %
ggplot(data=df, aes(x=Tratamiento, y=Aceitunas, fill=Color)) +
geom_bar(stat="identity", position=position_stack()) +
geom_text(aes(y = PosEtiq, label = Etiquetas), size=3, col="white") +
scale_fill_manual(values=c("darkgreen", "purple","black")) +
theme (text = element_text(size=14)) +
ggtitle ("Estado de maduracion de aceitunas \n con distintos tratamientos") +
theme (plot.title = TemaTitulo) +
theme (axis.title = element_text(face="bold", colour="blue", size=rel(1.5)))
df <- ddply(df, "Tratamiento", transform,
PosPorcentaje = cumsum(PorcentajeAce)-0.5*PorcentajeAce)
df
## Tratamiento Color Aceitunas PosEtiq PorcentajeAce Etiquetas
## 1 Testigo Verde 96 48 71.641791 72 %
## 2 Testigo Envero 18 105 13.432836 13 %
## 3 Testigo Negra 20 124 14.925373 15 %
## 4 Tratado Verde 30 15 20.270270 20 %
## 5 Tratado Envero 10 35 6.756757 7 %
## 6 Tratado Negra 108 94 72.972973 73 %
## PosPorcentaje
## 1 35.82090
## 2 78.35821
## 3 92.53731
## 4 10.13514
## 5 23.64865
## 6 63.51351
ggplot(data=df, aes(x=Tratamiento, y=PorcentajeAce, fill=Color)) +
geom_bar(stat="identity", position=position_stack()) +
geom_text(aes(y = PosPorcentaje, label = Etiquetas), size=3, col="white") +
scale_fill_manual(values=c("darkgreen", "purple","black")) +
theme (text = element_text(size=14)) +
ggtitle ("Estado de maduracion de aceitunas \n con distintos tratamientos") +
theme (plot.title = TemaTitulo) +
theme (axis.title = element_text(face="bold", colour="blue", size=rel(1.5)))
ggplot(data=df, aes(x=Tratamiento, y=PorcentajeAce, fill=Color)) +
geom_bar(stat="identity", position=position_stack()) +
geom_text(aes(y = PosPorcentaje, label = Etiquetas), size=3, col="white") +
scale_fill_manual(values=c("darkgreen", "purple","black")) +
theme (text = element_text(size=14)) +
ggtitle ("Estado de maduracion de aceitunas \n con distintos tratamientos") +
theme (plot.title = TemaTitulo) +
theme (axis.title = element_text(face="bold", colour="blue", size=rel(1.5))) +
guides(fill=guide_legend(reverse=TRUE)) +
labs(x = "Tratamientos",y = "Porcentaje de aceitunas (%)")