X-axis values in bold

Asked

Viewed 29 times

2

How to render the characters of the x-axis of the graph below in bold?

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, levels = c("Jun", "Jul")), y = Valor, fill = Variedades)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_label(aes(label = Valor), position = position_dodge(width = 1), show.legend = FALSE) +
  scale_fill_manual(values = c("cyan2", "lightgreen")) +
  labs(x = "Período", y = "", title = "TCH", subtitle = "Ganho de peso mensal") +
  scale_y_continuous(limits = c(0,60)) +
  theme_minimal() +
  theme(plot.title = element_text(vjust = 1.5, hjust = 0.5), 
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

Gráfico gerado

1 answer

2

Just put the argument axis.text.x = element_text(face = "bold") within the function theme.

The argument axis.text.x informs that the desired change must occur in the x-axis markings (make the parallel with the argument axis.text.y, also present in the creation of the chart) and element_text(face = "bold") informs that such markings shall be Bold (bold in English).

The code and graph with the result are below.

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, levels = c("Jun", "Jul")), y = Valor, fill = Variedades)) +
    geom_bar(stat = "identity", position = "dodge") +
    geom_label(aes(label = Valor), position = position_dodge(width = 1), show.legend = FALSE) +
    scale_fill_manual(values = c("cyan2", "lightgreen")) +
    labs(x = "Período", y = "", title = "TCH", subtitle = "Ganho de peso mensal") +
    scale_y_continuous(limits = c(0,60)) +
    theme_minimal() +
    theme(plot.title = element_text(vjust = 1.5, hjust = 0.5), 
                axis.title.y=element_blank(),
                axis.text.y=element_blank(),
                axis.ticks.y=element_blank(),
                axis.text.x=element_text(face = "bold"))

Created on 2021-07-22 by the reprex package (v2.0.0)

  • Thank you so much for the help! excellent! In this case the argument Axis.text. x can also act on the data label present in the graph bars?

Browser other questions tagged

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