简体   繁体   中英

Make X-Axis Lables, value labels and other axis and labels bold in Ggplot2

I work inside a research environment and I can't copy paste the code I used there, but I have previously generated this plot , and have been helped by various people in labelling it with the count number. The problem arises when I screenshot the plot from inside the research environment, and the legends are illegible. I am hoping I can address this by making the labels (including the X-axis label) all bold.

I used some mock-data outside the environment and this is what I have so far.

library(ggplot2)
library(reshape2)
md.df = melt(df, id.vars = c('Group.1'))
tmp = c("virginica","setosa","versicolor")
md.df2 = md.df[order(match(md.df$Group.1, tmp)),]
md.df2$Group.1 = factor(as.character(md.df2$Group.1), levels = unique(md.df2$Group.1))

ggplot(md.df2, aes(x = Group.1, y = value, group = variable, fill = variable)) +
  geom_bar(stat="identity",color='black', position = "dodge") +
  xlab('Species') + ylab('Values') + theme_bw()+
  ylim(0,8)+
  theme(text = element_text(size=16),
        axis.text.x = element_text(angle=0, hjust=.5),
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))+
  ggtitle("Order variables in barplot")+
  geom_text(aes(label=value), vjust=-0.3, size=4, # adding values
            position = position_dodge(0.9))+ element_text(face="bold")

I need to make the labels onto bold, and the element_text isn't working mainly because I am probably using it in the wrong way. I'd appreciate any help with this.

An example of this plot which I haven't been able to find mock data to re-create outside the environment, have asked a question about in the past , is the one where the axis ticks also need to be made bold. This is because the plot is illegible from the outside.

I've tried addressing the illegibility by saving all my plots using ggsave in 300 resolution but it is very illegible.

I'd appreciate any help with this, and thank you for taking the time to help with this. 在此处输入图像描述

As I mentioned in my comment to make the value labels bold use geom_text(..., fontface = "bold") and to make the axis labels bold use axis.text.x = element_text(angle=0, hjust=.5, face = "bold") .

Using a a minimal reproducible example based on the ggplot2::mpg dataset:

library(ggplot2)
library(dplyr)

# Create exmaple data
md.df2 <- mpg |>
  count(Group.1 = manufacturer, name = "value") |>
  mutate(
    variable = value >= max(value),
    Group.1 = reorder(Group.1, -value)
  )

ggplot(md.df2, aes(x = Group.1, y = value, group = variable, fill = variable)) +
  geom_col(color = "black", position = "dodge") +
  geom_text(aes(label = value), vjust = -0.3, size = 4, position = position_dodge(0.9), fontface = "bold") +
  labs(x = "Species", y = "Values", title = "Order variables in barplot") +
  theme_bw() +
  theme(
    text = element_text(size = 16),
    axis.text.x = element_text(angle = 90, vjust = .5, face = "bold"),
    plot.title = element_text(hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5)
  )

In addition to @stefan 's answer, you can also set tick length and thickness like so:

## ... plot code +
    theme(## other settings ...,
          axis.ticks = element_line(linewidth = 5),
          axis.ticks.length = unit(10, 'pt'))

However, the example "phenotype conditions" will probably remain hard to appraise, regardless of optimization on the technical level. On the conceptual level it might help to display aggregates, eg condition counts per Frequency and supplement a textual list of conditions (sorted alphabetically and by Frequency , or the other way round) for those readers who indeed want to look up any specific condition .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM