How to edit the following bar graph in R?

Asked

Viewed 37 times

-3

I would like to understand how I can make the following changes to the chart below:

  • Change the Y-axis scale and hide it from the graph if you want
  • Place values on top of each bar
  • Place the X-axis months in ascending order (June... July.) and bold the writing

I’m using the following script:

library(ggplot2)
library(tidyr)

TCH <- data.frame(Período =c("Jun", "Jul"), 
                         CV6654 = c(9.38, 26.14), CTC9002 = c(16.27, 47.71))

df <- gather(TCH, "Variedades", "Valor", -Período)

ggplot(df, aes(x = factor(Período), y = Valor, fill = Variedades)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("1", "3")) +
  labs(x = "Período", y = "")

gráfico gerado com o script acima

    1. Change to what? 2) see geom_text or geom_label; 3) x = factor(Período, levels = c("Jun", "Jul")).
  • Hello Patrick, welcome to Sopt. A few tips to get your question answered satisfactorily: 1) Be specific about the result you are looking for. Do you want to change the axis scale as? For logarithmic scale? Fraction? Percentage? Without specifying, you will get at most a generic response. 2) Divide your doubts into several posts. Ideally, each question should refer to a single problem. This assists other users when searching for a solution and helps you delimit the steps of your code. Even, if you look for separate steps, you will find answers on the site that may help you.

1 answer

1

 ggplot(df, aes(x = factor(Período), y = Valor, fill = Variedades)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("1", "3")) +
  geom_text(aes(y = Valor, label = Valor), vjust = -0.2, # COLOCAR VALORES EM CIMA DAS BARRAS
            position = position_dodge(width = 1)) +
  labs(x = "Período", y = "") +
  theme(axis.text.y = element_blank(), axis.ticks = element_blank(), # APAGAR EIXO Y
        axis.text.x = element_text(face="bold")) + # ROTULO EIXO X EM NEGRITO 
  scale_x_discrete(breaks=c("6", "7"),
                   labels=c("Junho", "Julho")) # REORDENANDO COLUNAS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.