0

A data frame I created from a csv I imported cannot be seen in ggplot2. I inspected the frame and I am pretty sure it's there but I am new to R. This is a frame that appeared to work with every function but when it came to this one it stopped working. This is my code:

    visualize(height_diff_under_null) +
      shade_p_value(obs_stat = obs_diff_position_height, direction = "both") +
      labs(x = "Difference in mean height for forwards and centers", y = "Count",
           title = "Null distribution of differences in forward and center height",
           subtitle = "Actual difference observed in the data is marked in red"
           )

enter image description here

Environment showing csv and the data frame:

enter image description here

After adding the code:

dput(head(height_diff_under_null, 20))

result:

structure(list(replicate = 1:20, stat = c(0.254006410256409, 
-0.362847222222229, -0.104166666666671, -0.0643696581196593, 
0.0152243589743648, 0.0550213675213627, -0.323050213675216, 0.114716880341874, 
0.0351228632478637, 0.174412393162399, 0.612179487179489, 0.174412393162399, 
-0.0444711538461604, -0.502136752136749, -0.00467414529914834, 
0.134615384615387, 0.0351228632478637, 0.492788461538467, -0.0842681623931583, 
-0.402644230769226)), row.names = c(NA, -20L), class = c("infer", 
"tbl_df", "tbl", "data.frame"), response = height, explanatory = position, response_type = "numeric", explanatory_type = c(`explanatory_variable(x)` = "factor"), distr_param = c(df = 151.819892587405), null = "independence", theory_type = "Two sample t", generated = TRUE, type = "permute", hypothesized = TRUE, formula = height ~ 
    position, fitted = FALSE, stat = "diff in means")

So I tried to use that code and I'm not sure if I used it right

library(tidyverse)

height_diff_under_null %>% 
  ggplot(aes(x = stat)) +
  geom_histogram(bins = 10) +

  visualize(height_diff_under_null) +
  shade_p_value(obs_stat = obs_diff_position_height, direction = "both") +
  labs(x = "Difference in mean height for forwards and centers", y = "Count",
       title = "Null distribution of differences in forward and center height",
       subtitle = "Actual difference observed in the data is marked in red"
       )

The result:

Error in `ggplot_add()`:
! Can't add `visualize(height_diff_under_null)` to a ggplot object.
Backtrace:
 1. ggplot2:::`+.gg`(...)
 2. ggplot2:::add_ggplot(e1, e2, e2name)
 4. ggplot2:::ggplot_add.default(object, p, objectname)
Error in ggplot_add(object, p, objectname) :

The error I received after adding to the top:


  height_diff_under_null %>% 
  ggplot(aes(x = stat)) +
  geom_histogram(bins = 10) +

  visualize(height_diff_under_null) +
  shade_p_value(obs_stat = obs_diff_position_height, direction = "both") +
  labs(x = "Difference in mean height for forwards and centers", y = "Count",
       title = "Null distribution of differences in forward and center height",
       subtitle = "Actual difference observed in the data is marked in red"
       )

The error:

Error in `ggplot_add()`:
! Can't add `visualize(height_diff_under_null)` to a ggplot object.
Backtrace:
 1. ggplot2:::`+.gg`(...)
 2. ggplot2:::add_ggplot(e1, e2, e2name)
 4. ggplot2:::ggplot_add.default(object, p, objectname)
Error in ggplot_add(object, p, objectname) :

Pic of error:

enter image description here

After testing Mark's code: Mark's code in new file

Photos for Phil libraries Environment

  • hi travis! please provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Mark Jun 30 '23 at 03:51
  • Hello Mark, the purpose is to get a p-value I can show you the code I based this on and what I did. visualize(gpa_diff_under_null) + shade_p_value(obs_stat = obs_diff_gpa_sex, direction = "both") + labs(x = "Difference in mean GPA for females and males", y = "Count", title = "Null distribution of differences in female and male GPAs", subtitle = "Actual difference observed in the data is marked in red" ) – travis irvin Jun 30 '23 at 03:58
  • I cant add a pic in here to sow you what the graph should look like, I've been at this for hours any help would be appreciated. – travis irvin Jun 30 '23 at 04:01
  • Please put the code and the data in your question, not the comments Travis! – Mark Jun 30 '23 at 04:08
  • ```{r} visualize(height_diff_under_null) + shade_p_value(obs_stat = position_height, direction = "both") + labs(x = "Difference in mean height for forwards and centers", y = "Count", title = "Null distribution of differences in forward and center height", subtitle = "Actual difference observed in the data is marked in red" ) ``` – travis irvin Jun 30 '23 at 04:09
  • This was the original csv: nba_center_forward and I created a new frame off of that position_height – travis irvin Jun 30 '23 at 04:10
  • once again, ***do not put the data or code in the comments***. Replace the photos in your question with the actual code and the actual data, enough that I can reproduce your problem on my pc. – Mark Jun 30 '23 at 04:11
  • 1
    Oh my bad I'll do it now – travis irvin Jun 30 '23 at 04:12
  • It's fixed and I added a pic of the original code – travis irvin Jun 30 '23 at 04:27
  • that is better, thank you for doing that. We now have the code, but not the data. Can you run `dput(head(height_diff_under_null, 20))` in your RStudio console and add the output to your question? Thanks! – Mark Jun 30 '23 at 04:33
  • Thanks Mark, I ran the command and added the result. – travis irvin Jun 30 '23 at 04:41
  • It would be helpful if you provide what packages you are calling. It's not clear to me where this `visualize()` function is coming from, and why you'd expect to work with other `ggplot2` code. – Phil Jun 30 '23 at 05:25
  • Hey Phill I added it in pictures – travis irvin Jun 30 '23 at 05:36

1 Answers1

0

I'm not sure what is going on with the height_diff_under_null data you provided...it doesn't seem to run! Quite odd. Anyway, I created this with ggplot to illustrate. Hopefully when you run it you can see the histogram:

# copy from here
new_data<- data.frame(replicate = 1:20, stat = c(0.254006410256409, 
-0.362847222222229, -0.104166666666671, -0.0643696581196593, 
0.0152243589743648, 0.0550213675213627, -0.323050213675216, 0.114716880341874, 
0.0351228632478637, 0.174412393162399, 0.612179487179489, 0.174412393162399, 
-0.0444711538461604, -0.502136752136749, -0.00467414529914834, 
0.134615384615387, 0.0351228632478637, 0.492788461538467, -0.0842681623931583, 
-0.402644230769226))

library(tidyverse)

new_data %>% 
  ggplot(aes(x = stat)) +
  geom_histogram(bins = 10)
# to here
Mark
  • 7,785
  • 2
  • 14
  • 34
  • Hey Mark, I've been having issues all week, but I'm not sure I ran it right but I still got an error – travis irvin Jun 30 '23 at 05:05
  • what error? can you copy and paste it in a comment? – Mark Jun 30 '23 at 05:07
  • Hey Mark, I added it. – travis irvin Jun 30 '23 at 05:16
  • did you run the entirety of the code in my bit (including the `height_diff_under_null <- data.frame...` part)? Because you are still running the `visualize` function, which as far as I'm aware isn't a ggplot function – Mark Jun 30 '23 at 05:18
  • Yes... I think so the command in the pic is what I ran. Is there something else I need to add? – travis irvin Jun 30 '23 at 05:21
  • open a new file, and paste in ***only what I included in my answer, none of your code***. Then run each line, and tell me if it shows a plot or not. There was a small typo in it, so maybe copy it again – Mark Jun 30 '23 at 05:23
  • Hey, Mark I added it in a picture – travis irvin Jun 30 '23 at 05:29
  • okay.... try this – Mark Jun 30 '23 at 05:32
  • Hey Mark I cant see anything – travis irvin Jun 30 '23 at 05:37
  • I updated the stuff in the answer – Mark Jun 30 '23 at 05:49
  • Hey Mark that actually worked – travis irvin Jun 30 '23 at 05:56
  • cool! I don't want to dissuade you, but you might be better off working through a tutorial, using a well-known dataset, before tackling your own datasets. That way, you will have some understanding of how `ggplot` works, how things are meant to be structured, and what the basic functions do – Mark Jun 30 '23 at 06:10
  • [This chapter](https://r4ds.had.co.nz/data-visualisation.html) of a book is highly recommended. If you'd prefer not to read things, and just want code that works, there are websites like [The R Graph Gallery](https://r-graph-gallery.com/272-basic-scatterplot-with-ggplot2.html) – Mark Jun 30 '23 at 06:14
  • 1
    Mark I completely agree with you and if I had a choice I would not have made my own data set then tried to use it. I will work through this tutorial though and I really appreciate it. – travis irvin Jun 30 '23 at 06:20
  • best of luck! Feel free to leave me a message or question if I can be of any help – Mark Jun 30 '23 at 06:32