最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - gghighlight and facet_wrap one always highlighted - Stack Overflow

programmeradmin1浏览0评论

First time poster, so I'm sorry if I made any mistakes!

I'm trying to make a small multiples plot in ggplot2 in R, using the gghighlight package. I have the temperature over time in multiple locations, and the temperature of the surroundings. My goal is to have 1 location highlighted in each plot (and all other locations in the background), but also to have the temperature of the surroundings highlighted in each plot. Does anyone know how to do this?

So in the following example code:

    library(ggplot2)
    library(gghighlight)

    # Example dataset
    set.seed(42)
    data <- data.frame(
    day = rep(1:100, 6),
    temp = rnorm(600, mean = 20, sd = 5),
    location = rep(c("Location_1", "Location_2", "Location_3", "Location_4", 
    "Location_5", "surrounding"), each = 100)
    )


    # Plot
    ggplot(data, aes(x = day, y = temp, color = location)) +
    geom_line() +
    facet_wrap(~location, ncol = 3) +
    gghighlight()

my image

But then not have a separate plot of surroundings, but have surroundings be highlighted in every plot.

I tried to add a line for surrounding after the gghighlight() function, like so:

     ggplot(data, aes(x = day, y = temp, color = location)) +
     geom_line(data = subset(data, location != "surrounding")) + 
     facet_wrap(~location, ncol = 3) +
     gghighlight() +
     geom_line(data = subset(data, location == "surrounding"))

But that didn't change anything in the plot.

First time poster, so I'm sorry if I made any mistakes!

I'm trying to make a small multiples plot in ggplot2 in R, using the gghighlight package. I have the temperature over time in multiple locations, and the temperature of the surroundings. My goal is to have 1 location highlighted in each plot (and all other locations in the background), but also to have the temperature of the surroundings highlighted in each plot. Does anyone know how to do this?

So in the following example code:

    library(ggplot2)
    library(gghighlight)

    # Example dataset
    set.seed(42)
    data <- data.frame(
    day = rep(1:100, 6),
    temp = rnorm(600, mean = 20, sd = 5),
    location = rep(c("Location_1", "Location_2", "Location_3", "Location_4", 
    "Location_5", "surrounding"), each = 100)
    )


    # Plot
    ggplot(data, aes(x = day, y = temp, color = location)) +
    geom_line() +
    facet_wrap(~location, ncol = 3) +
    gghighlight()

my image

But then not have a separate plot of surroundings, but have surroundings be highlighted in every plot.

I tried to add a line for surrounding after the gghighlight() function, like so:

     ggplot(data, aes(x = day, y = temp, color = location)) +
     geom_line(data = subset(data, location != "surrounding")) + 
     facet_wrap(~location, ncol = 3) +
     gghighlight() +
     geom_line(data = subset(data, location == "surrounding"))

But that didn't change anything in the plot.

Share Improve this question edited Nov 20, 2024 at 9:31 stefan 126k6 gold badges38 silver badges75 bronze badges Recognized by R Language Collective asked Nov 20, 2024 at 7:59 Laura VerbeekLaura Verbeek 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Basically your approach with subset is right. However, you have to drop or rename the column used for facetting for the geom_line which you want to display in each facet:

library(ggplot2)
library(gghighlight)

library(ggplot2)
library(gghighlight)

ggplot(mapping = aes(x = day, y = temp, color = location)) +
  geom_line(
    data = subset(data, location != "surrounding")
  ) +
  geom_line(
    data = subset(data, location == "surrounding", select = -location),
    aes(color = I("grey40"))
  ) +
  facet_wrap(~location, ncol = 3) +
  gghighlight()
#> label_key: location

发布评论

评论列表(0)

  1. 暂无评论