开发者

ggplot will not show plot when custom tooltip

开发者 https://www.devze.com 2022-12-07 21:42 出处:网络
So I was playing around with the babynames dataset with the intention of practicing generating plots in plotly.

So I was playing around with the babynames dataset with the intention of practicing generating plots in plotly.

library(babynames)
library(dplyr)
library(ggplot2)
library(plotly)
nms <- babynames %>% filter(name == "Alex" | name == "Leslie")

This code works well, but will not let you customize your text tooltip

fig <- nms %>% ggplot() + aes(x = year, y = proportion, col = name,
                              group=interaction(sex,name),
                              linetype= sex) +
                              geom_line()
fig

When we define a text argument in aes(...) ggplot does not know how to connect the points.

fig <- nms %>% ggplot() + aes(x = year, y = proportion, col = name,
                              group=interaction(sex,name),
                              linetype= sex, text= paste0("foo:",prop)) +
                              geom_line()
fig

My solution to get things working was to define a group variable; however, I am confused as to why the addition of the text= argument was a fatal error?

fig <- nms %>% ggplot() + aes(x = year, y = proportion, col = name,
                              group=interaction(sex,name),
                              linetype= sex, text= paste0("foo:",prop)) +
                              geom_line()
fig
ggplotly(fig, tooltip="text")

The reason for this post is twofold. Firstly, I did not see anyone with my issue online - thus I would like to provide a solution. Secondly, I would like to know why this behavior is shown.

I was expecting that the text= argument would be ignored and that the plot should either pass or fail consistently whether or not it was present.

I understand that it is necessary to use the group= argument in order to explicitly define a group, but I do not know why the code fails only when the text= is introduced. Any insight would be appreciated. I have looked into the source code and have not found any answer开发者_开发百科s.


Your code does not work. Reason is that you add aesthetics aes directly to a ggplot -object. You need to put aes into ggplot.

Second reason: your variable is called prop not proportion.

The DF has more than 500 rows. So it does not makes sense to name every single point with text. I have created a little helper DF as a subset of the original DF. In geom_text I refer to this newly built DF and define label with the variable name. The limits of the x-axis are extended so that the text fits at the end of each line.

Please, take the following code as a starting point for further tuning to your desire.

library(tidyverse)
library(babynames)

nms <- babynames %>% filter(name == "Alex" | name == "Leslie")

# subset of noms for use in geom_text
for_geom_text <- nms |>
  filter(year == max(year)) |> 
  mutate(year = year + 25)

fig <- nms %>%
  ggplot(aes(
    x = year, y = prop, colour = name,
    group = interaction(sex, name),
    linetype = sex
  )) +
  geom_line() +
  geom_text(
    data = for_geom_text,
    aes(year, y = prop, label = paste("foo: ", name))
    ) + 
  xlim(1880, 2060)

fig

ggplot will not show plot when custom tooltip

PS: I would recommend to look in one of many good books about ggplot. For example this

0

精彩评论

暂无评论...
验证码 换一张
取 消