1

I have this code for making a ggplot, which works fine (MRE with a built-in dataset):

mydata <- airquality
avg <- mean(mydata$Wind)
stde <- sd(mydata$Wind)
qup <- qnorm(p = 0.95,mean = avg, sd = stde)
qdown <- qnorm(p = 0.05,mean = avg, sd = stde)
N95 <- length(mydata$Wind[mydata$Wind < qup & mydata$Wind > qdown])
mydata$date <- as.Date(paste(mydata$Month,"-",mydata$Day,sep = ""),format = "%m-%d")

library(ggplot2)
g <- ggplot(mydata, aes(x = date, y = Wind)) + geom_point(aes(colour = "measurements"),pch = 16) +
  geom_hline(aes(yintercept = avg,colour = "mean"),lty = "solid",lwd = 1.2) +
  geom_hline(aes(yintercept = qup,colour = "95%-quantile"),lty = "dotted",lwd = 1.2) +
  geom_hline(aes(yintercept = qdown,colour = "5%-quantile"),lty = "dotted",lwd = 1.2) +
  scale_colour_manual(name = "", values = c(measurements = "deepskyblue4",mean = "goldenrod","95%-quantile" = "goldenrod","5%-quantile" = "goldenrod"),
                      guide = guide_legend(override.aes = list(linetype = c("blank","solid","dotted","dotted"), shape = c(16,NA,NA,NA)))) +
  ylab("Wind [mph]") + xlab("date") + ggtitle("wind speed") +
  scale_y_continuous(limits = c(0,25)) +
  labs(caption = paste("N = ", length(mydata$Wind), "; Mean = ", round(avg,digits = 3), "; Sd = ", round(stde,digits = 3), "; N within limits = ", N95, sep = "")) +
  theme(axis.line.x.bottom = element_line(size=1.5),axis.line.y.left = element_line(size=1.5),plot.title = element_text(hjust = 0.5), axis.title = element_text(size = rel(1.4)),legend.text = element_text(size = rel(1.4)),legend.title = element_text(size = rel(1.3)),axis.text = element_text(size = rel(1.2)))

print(g)

However when I want to make this plot interactive by calling print(plotly::ggplotly(g)), the caption defined in labs is not displayed. Any ideas what causes this or how to fix it?

Many thanks!

king_of_limes
  • 359
  • 1
  • 11
  • When using `ggplotly` you have to keep in mind that `ggplot2` and `plotly` are two different things. `ggplotly` does its best to create a mapping of a `ggplot` object to a `plotly` object. However, this mapping will not be perfect, more or less, as plotly does not offer all the `ggplot2` features and vice versa. But as an option to manually add a caption have a look [plotly adding a source or caption to a chart](https://stackoverflow.com/questions/45103559/plotly-adding-a-source-or-caption-to-a-chart). – stefan Apr 29 '22 at 09:59
  • Thanks for the input. I tried using `annotate("text",x=1,y=-0.1, label= paste("[...]"))` instead of `labs`, but now the ggplot command throws the error _Invalid input: date_trans works with objects of class Date only_, which I find weird, because the annotation should have nothing to do with the scale or limits of the axis. Also, `class(mydata$date)` returns `Date`, but I've had my share of problems with time axes before. – king_of_limes Apr 29 '22 at 10:22
  • yeah. But `x=1` is not a date object, i.e. you have to use a Date for the x coordinate of the label, e.g. `x = as.Date("2022-04-29")`. – stefan Apr 29 '22 at 10:44
  • Ah, that makes sense. This works well for plotting inside the graph, which will do for now. – king_of_limes Apr 29 '22 at 10:57

0 Answers0