0

Using ggplot2 in R, i'm trying to insert a red line that indicates the average of a chain. I would like to insert the average value close to the line so that it was not necessary to "deduct" the value.

I tried to use a negative coordinate for x, but it did not work, the value is behind the axis.

ggplot(data = chain.fmBC) + 
  geom_line(aes(1:25000, chain.fmBC$V2)) +
  labs(y = "", x = "") + 
  labs(caption= "Bayes C") + 
  geom_hline(yintercept = mean(chain.fmBC$V2), colour = "RED") +
  geom_text(label = round(mean(chain.fmBC$V2), 2), 
            x = 0, y = min(chain.fmBC$V2), colour = "RED")

this is a picture of my graph:

from https://i.imgur.com/hc0K9xh.jpg

How could I put the value that is in red (media) to the left of the y-axis of the graph, between 0 and 5000, as if it were a label of the y-axis?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Might be a starting point to look at `ggrepel`: https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html – Jonny Phelps Apr 18 '19 at 14:35
  • 1
    You could consider adding it as an axis break and then changing the color along the lines of [this](https://stackoverflow.com/questions/38862303/customize-ggplot2-axis-labels-with-different-colors) – aosmith Apr 18 '19 at 14:41
  • You could also add an [annotation](https://ggplot2.tidyverse.org/reference/annotate.html). – Wil Apr 18 '19 at 14:45
  • Also related - [turning of clipping for labels](https://stackoverflow.com/q/30061563/903061) – Gregor Thomas Apr 18 '19 at 15:04
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a workable sample of data. – camille Apr 18 '19 at 15:33

2 Answers2

0

You can set your y axis ticks manually so that it includes the mean value. This will give you a nicely positioned annotation. If the real issue is the colored axis label, unfortunately this does not solve that

Example:

ggplot(mtcars, aes(disp)) +
  geom_histogram() +
  geom_hline(yintercept = 0.5, color = "red") +
  scale_y_continuous(breaks = c(0,0.5,1,2,3,4)) +
  theme(axis.text.y = element_text())

Which will give you this:

img

camille
  • 16,432
  • 18
  • 38
  • 60
aakosm
  • 1
0

I was successful following the suggestions, I would like to share. I got good help here.

cadeia.bayesc <- ggplot(data = chain.fmBC) + geom_line(aes(1:25000, chain.fmBC$V2)) +
        theme(plot.margin = unit(c(0.5,0.5,0.5,1), "lines")) +  # Make room for the grob
        labs(y = "", x = "") + labs(caption= "Bayes C") + 
cadeia.bayesc <- cadeia.bayesc + geom_hline(yintercept = mean(chain.fmBC$V2), colour = "RED") # insert the line

cadeia.bayesc <- cadeia.bayesc + annotation_custom( # grid::textgrob configure the label 
                grob = textGrob(label = round(mean(chain.fmBC$V2),2), hjust = 0, gp = gpar(cex = .7, col ="RED")),
                xmin = -6000, xmax = -100, ymin = mean(chain.fmBC$V2), ymax = mean(chain.fmBC$V2))

# Code to override clipping
cadeia.bayesc.plot <- ggplot_gtable(ggplot_build(cadeia.bayesc))
cadeia.bayesc.plot$layout$clip[cadeia.bayesc.plot$layout$name == "panel"] <- "off"
grid.draw(cadeia.bayesc.plot)

result (https://i.stack.imgur.com/2FG0m.jpg)