dnmc
dnmc

Reputation: 107

assigning geom_hline value based on filtered data, and doing this as part of the same code chunk?

I'm trying assign geom_hline based on filtered data and selection of value from one column, but doing this all within the same code chunk. I am not sure what the best way is go about it - any help would be much appreciated.

sample data:

structure(list(sample_name = c("control1", "control2", "S01", 
"S02", "S03", "S04", "S05", "S06", "S07", "S08"), estimate = c(1.703, 
5.553, 4.851, 5.257, 4.573, 3.278, 1.687, 3.628, 1.877, 5.826
), std.error = c(1.767, 2.382, 1.641, 1.062, 1.133, 1.477, 0.978, 
0.611, 1.893, 0.78), upper_limit_value = c(5.166, 10.223, 8.067, 
7.339, 6.795, 6.173, 3.605, 4.825, 5.586, 7.355), lower_limit_value = c(-1.761, 
0.884, 1.635, 3.175, 2.352, 0.384, -0.231, 2.431, -1.833, 4.298
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
df%>%
  ggplot(., aes(x=sample_name, y=estimate, group=sample_name, color=sample_name))+ 
  geom_point() +
  geom_hline(???)

within the geom_hline() part, is there a way to define what the y-intercept should be based on filtered data - almost like a side assignment?

something along the lines of

geom_hline(aes(yintercept = df %>% filter(sample_name="control1") %>% select("upper_limit_value"))) +
geom_hline(aes(yintercept = df %>% filter(sample_name="control1") %>% select("lower_limit_value")))

In this case, there's only one row of filtered data for sample_name="control1", and I'm trying to use the value under the "upper_limit_value " column (and "lower_limit_value " value as a separate geom_hline).

Thanks!

Upvotes: 0

Views: 1007

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76673

Try to subset the data in the data argument:

geom_hline(data = df %>% filter(sample_name == "control") %>% select(upper_limit_value),
           mapping = aes(yintercept = upper_limit_value))

The complete code would then become the following.

df %>%
  ggplot(aes(x=sample_name, y=estimate, group=sample_name, color=sample_name))+ 
  geom_point() +
  geom_hline(data = df %>% filter(sample_name == "control") %>% select(upper_limit_value),
             mapping = aes(yintercept = upper_limit_value))

(I don't believe there's need to select(upper_limit_value) but without test data it's not easy to say.)

Another option is to use pull in the question's code, not select. The difference is that select returns a data set with those columns and pull returns the columns values.

geom_hline(aes(yintercept = df %>% filter(sample_name == "control") %>% pull(upper_limit_value)))

Edit

The data transformation I am refering to above is

df$sample_name[df$sample_name == "control2"] <- "control"

Then the filter in geom_hline returns a data set with 1 row.

After this comment I have tested the code with the posted data (but after changing "control2" to "control") and everything works as expected.

I would also suggest that the initial pipe of df into ggplot is not needed, since no transformations are to be applied to the data set.

ggplot(df, aes(x=sample_name, y=estimate, group=sample_name, color=sample_name))+ 
  geom_point() +
  geom_hline(aes(yintercept = df %>% filter(sample_name == "control") %>% pull(upper_limit_value)))

Upvotes: 1

Related Questions