1

I'm pretty new to R and SO and would appreciate any help you may have on this.

I have a factor class data with 17 levels. I'm using library(ggplot2) and geom_bar(position="fill") to create a proportional bar plot. Code and output / plot are below.

Basically, it's fine except what I would like to be able to do is create 17 more of these plots with a way to highlight one of the levels (i.e. one of the colors) remain the same and the rest to be greyed out as a way to distinguish one level from the rest. Because there are 17 levels and the colors are pretty similar, it's difficult to tell some levels apart right now.

I hope that makes sense -- happy to edit and provide more information. I'd appreciate any pointers or help on this one. Thank you so much!


Code

# libraries
library(tidyverse) # for the plot
library(ggplot2) # for the plot
library(scales) # for the x-axis scaling
library(lubridate) # for the "POSIXct" and "POSIXt" class

#data classes
class(df.forum$p.date) # "POSIXct" "POSIXt"
class(df.forum$p.forum) # factor

# the plot
df.forum %>% 
  ggplot(aes(x = p.date, fill = factor(p.forum))) +
  geom_bar(position = "fill", stat = "count", show.legend = TRUE) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust = 0.2)) +
  scale_x_datetime(date_breaks = "1 month",labels = date_format("%b %Y"), limits = c(mdy_hms("10/1/13 20:00:00"),mdy_hms("5/1/14 20:00:00")))

Plot

ggplot2 proportional bar graph plotted

Edits / Reproducible Example

I also tried to set up a reproducible example and seems like there may be issues here:

    # data
d <- as.POSIXct(
  c("2020-01-01", "2020-01-01","2020-01-01", 
    "2020-01-02", "2020-01-02", "2020-01-02", 
    "2020-01-03", "2020-01-03", "2020-01-03"))

t <- as.factor(
  c("ATopic", "BTopic", "CTopic",
    "CTopic", "BTopic", "BTopic",
    "CTopic", "ATopic", "BTopic"))

df <- data.frame(d, t)

# the plot
df %>% 
  ggplot(aes(x = d, fill = factor(t))) +
  geom_bar(position = "fill", stat = "count") 

##E rror line: position_stack requires non-overlapping x intervals

This produces the following plot with the error "position_stack requires non-overlapping x intervals": enter image description here


Edit: applied gghilight as suggested and it worked! Here's the solution I was given with facet_wrap() and gghighlight() for folks.

I also tried the following code:

library(gghighlight)
df %>% 
  ggplot(aes(x = factor(d), fill = factor(t))) +
  geom_bar(position = "stack", stat = "count") +
  facet_wrap(~t) +
  gghighlight()

And here's the output: enter image description here

Community
  • 1
  • 1
bob
  • 117
  • 1
  • 1
  • 8

1 Answers1

2

I think the use gghighlight (in combination of facet_wrap) will fit your need.

Here an example using iris dataset to plot Sepal.Width in function of Sepal.Length for each species. Here I used facet_wrap to separate each species and gghighlight in order to show color only for one species at a time

library(ggplot2)
library(gghighlight)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
  geom_point()+
  facet_wrap(~Species)+
  gghighlight()

enter image description here

Does it answer your question ?

If not, please consider providing a reproducible example of your dataset (How to make a great R reproducible example)

Community
  • 1
  • 1
dc37
  • 15,840
  • 4
  • 15
  • 32
  • Absolutely. That's exactly what I wanted. Thank you so much @dc37! Accepted answer and +1. – bob Apr 11 '20 at 19:46
  • I tried this with facet on `p.forum` which has 17 levels. When I do, I lose the context of other levels. Is this different for geom_bar? I'll try to provide a reproducible example in the meantime. – bob Apr 11 '20 at 19:52
  • Examples added to post. And I got it with your help! Thanks again for the gghighlight package suggestion -- didn't know about that one. @dc37 – bob Apr 11 '20 at 20:35
  • Glad that my answer helps you to figure it out the solution to your problem ;) – dc37 Apr 11 '20 at 20:45