0

So my current plot looks like this: enter image description here

But I want to add one or two other series of text to the plot to look something like this:

enter image description here

Is that even possible to do such a thing using ggplot or should I go after another package?

Update: Here is the code for my current plot:

ggplot() + geom_col(data = s, aes(x = V1, y = V3, fill = V3), show.legend = F) + 
coord_flip() + 
labs(x = "", y = "") + 
scale_fill_gradient(low = "yellow", high="darkgreen") + 
theme(axis.text.y = element_text(vjust = 0, hjust = 0), plot.margin = margin(1, 0.3, 0.1, 0.3, "cm"), panel.background = element_rect(fill = 'white', colour = 'white'), axis.ticks.y = element_blank()) + ylim(0, 6)

and here is some fake data:

1  a  b  6  7
2  c  d  8  9
3  g  f  5  4
4  f  n  3  2

and here is the output of the real data:

structure(list(V1 = c("regulation of locomotion", "regulation of cell migration", 
"skin development", "negative regulation of biological process", 
"cell adhesion", "response to oxygen-containing compound"), V2 = c(7.74e-05, 
0.000143, 0.000165, 0.000176, 0.00019, 0.00019), V3 = c(4.111259039, 
3.844663963, 3.782516056, 3.754487332, 3.721246399, 3.721246399
)), row.names = c(NA, 6L), class = "data.frame")
stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    Sure is this possible using ggplot2. And there are several approaches to do so. But at least we need [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data and some code to start with. – stefan Feb 09 '23 at 19:49
  • @stefan thanks for the response, I've added the code to the question. – Amir Valizadeh Feb 09 '23 at 19:50
  • 1
    We still need some data. Including the columns with the additional values you want to show as "axis text". Simply type `dput(head(data))` in the console and copy the output into your post. – stefan Feb 09 '23 at 19:54
  • 1
    @stefan done, sorry for the inconvinience – Amir Valizadeh Feb 09 '23 at 19:57

1 Answers1

1

In case of just one column one of the easiest options would be to add the second column for your axis text via a geom_text layer like so. You only have to make sure to set the right size and color as for the axis text:

library(ggplot2)

ggplot(data = s) +
  geom_col(aes(x = V3, y = V1, fill = V3), show.legend = F) +
  geom_text(aes(x = -1, y = V1, label = scales::label_scientific()(V2)),
    hjust = 0, size = .8 * 11 / .pt, vjust = 0, color = "grey30"
  ) +
  labs(x = "", y = "") +
  scale_fill_gradient(low = "yellow", high = "darkgreen") +
  theme(
    axis.text.y = element_text(vjust = 0, hjust = 0),
    plot.margin = margin(1, 0.3, 0.1, 0.3, "cm"),
    panel.background = element_rect(fill = "white", colour = "white"),
    axis.ticks.y = element_blank()
  ) +
  xlim(-1, 6)

stefan
  • 90,330
  • 6
  • 25
  • 51