I'm new to the world of programming in R for data analysis and I have some questions about the ggplot library.
The graph I generated was this. I'm having difficulty in changing the column captions (or colors) on the right of the chart. In other words, where there is "mediaIntegralAmpla", "mediaIntegralCotas", "mediaParcialAmpla" and "mediaParcialCotas" I would like to replace it with "Integral ampla", "Integral cotas", "Parcial ampla" and "Parcial cotas".
Here is an image of my dataframe and I leave here the current code below. How i can change this descriptions?
I'm Brazilian and new to this type of problem description in R language. If you need more information or something doesn't make much sense, please point it out to me and I'll try to describe it better or provide other data. Thanks in advance!
library(ggplot2)
grafico.media <- ggplot(transposto, aes(x = areaDoConhecimento, y = media, fill = nome_media)) + geom_bar(stat = "identity", position = "dodge") +
labs(title = "Média das notas de corte por Área do Conhecimento",
x = "Área de Conhecimento",
y = "Média das notas", fill = "Legenda") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + # rotaciona a legenda do eixo x
scale_y_continuous(breaks = seq(0, 600, by = 200)) # Define intervalo de 500000 no eixo y
print(grafico.media)
I try to add another line like this
grafico.media <- geom_bar(aes( y = mediaIntegralAmpla, fill = "Integral ampla")
But it doenst change. I also tried adding a line to the labs, but it didn't work out very well. But I'm sure the syntax wasn't suitable.
There are some issues with your code: Here is the corrected code:
Note is mediaIntegralCotas
is not medialntegralCotas
and transform ,
to .
in media.
Then one way is to transform to factor type and add the desired labels:
library(dplyr)
library(ggplot2)
df %>%
mutate(media = as.numeric(gsub(",", ".", media))) %>%
mutate(nome_media = factor(nome_media, levels = c("medialntegralAmpla",
"mediaIntegralCotas",
"medialntegralCotas",
"mediaParcialAmpla",
"mediaParcialCotas"), labels = c("Integral ampla",
"Integral cotas",
"Integral cotas",
"Parcial ampla",
"Parcial cotas"))) %>%
ggplot(aes(x = areaDoConhecimento, y = media, fill = nome_media)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Média das notas de corte por Área do Conhecimento",
x = "Área de Conhecimento",
y = "Média das notas", fill = "Legenda") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
scale_y_continuous(breaks = seq(0, 600, by = 200))