0

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. enter image description here

Ar1229
  • 131
  • 2
  • 9
  • IMHO the main issue with your chart is that there are too many categories on the x-axis. But to make your labels bold try `geom_text(..., fontface = "bold")`. `element_text` is just for theme elements, i.e. remove `+ element_text` – stefan Nov 24 '22 at 17:54
  • ... but for the axis labels do `axis.text.x = element_text(angle=0, hjust=.5, face = "bold")` – stefan Nov 24 '22 at 17:56
  • Hi stefan, I tried geom_text(..., fontface = "bold") and received an error = "ignoring unknown parameters" – Ar1229 Nov 24 '22 at 18:01
  • I don't understand why you can't share the data shown on your plot. You have already put the plot in the public domain, and it shows all the data required to recreate the plot. Why not share the columns of data shown in the plot? I could sit and reverse engineer your data frame from your plot and include it in an answer, but it would be tedious. – Allan Cameron Nov 24 '22 at 18:48
  • The data on here is a screenshot, I can't bring it out or take it back in, which is why I'm using mock data. I can upload screenshots of my code, but that has previously been unhelpful – Ar1229 Nov 24 '22 at 19:16

2 Answers2

2

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)
  )

stefan
  • 90,330
  • 6
  • 25
  • 51
0

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, e. g. 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.

I_O
  • 4,983
  • 2
  • 2
  • 15