2

A couple of weeks ago, I drew a ggplot density plot in r. It worked fine. And then yesterday, I revisited and ran the same code. There was absolutely no change in the body of the code (only change in the Rmarkdown formatting like changing font size of texts, echo = FALSE, etc). I simply re-ran the same code and for some reason it does not work.

Here's the code for the dataset:

m_hospitals = m_hospitals %>% group_by(Provider.Name) %>% summarise(accSum = 
sum(Average.Covered.Charges), atpSum = sum(Average.Total.Payments), ampSum = 
sum(Average.Medicare.Payments), accMean = mean(Average.Covered.Charges), 
atpMean = mean(Average.Total.Payments), ampMean = 
mean(Average.Medicare.Payments)) 

#Take a sample
set.seed(1219)
sample_m_hospitals = m_hospitals[sample(nrow(m_hospitals), 30), ]

And here's the code for the plot that worked before but not anymore:

ggplot(data = sample_m_hospitals) +
aes(x = Provider.Name, y = accMean) + 
geom_density(alpha = .75) + theme(axis.text.x = element_text(angle = 45, 
hjust = 1))

It's giving me this message, "Groups with fewer than two data points have been dropped" * 30. It is true that each of 30 rows only has 1 observation because they are summarised. Funny thing is, the message did not pop up before, did not drop any data points and worked fine. I even have the screenshot I took for the plot drawn. Here's the link: 1

One change I feel suspicious about is updating R (updated before re-plotting, 3.5.0 > 3.5.1) which erased all my libraries that I had to re-install all of them. But I'm not sure if the update has to do anything with this issue. It worked fine, but why is it suddenly not working? I don't understand.. Please help! I just wanna plot this in similar form however possible.

Contents updated as per you guys' comments:

Screenshots for devtools::session_info() 2 , 3

Screenshots for sample_m_hospitals 4 , 5

JH P
  • 21
  • 1
  • 3
  • Without data we can't troubleshoot. Provide `dput()` of `sample_m_hospitals` so we can see what you are working with. `ggplot2` was recently majorly updated which may now raise more warnings than it used to. What versions of packages are you on? Provide `devtools::session_info()` – Calum You Jul 16 '18 at 18:56
  • 1
    Actually I don't even know how this worked in the first place - `geom_density` is supposed to be used on continuous x. – Calum You Jul 16 '18 at 18:58
  • @CalumYou I ran dput(sample_m_hospitals), and devtools::session_info(). And it's giving me huge lines of result. Instead of copying-pasting, I will upload screenshots. – JH P Jul 16 '18 at 19:06
  • Just uploaded the screenshots. I uploaded View(sample_m_hospitals) instead of dput(sample_m_hospitals) bc the latter is giving me hundreds of lines. Is there any particular ways you guys use to upload things like the result of dput()? – JH P Jul 16 '18 at 19:22
  • 1
    It's unlikely that anyone will type up data from a screenshot. You can either use `styler` to clean up the `dput` output and remove newlines so it fits on one line, or provide the least data that still reproduces the error (why not only include the two columns you are plotting?). See [how to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This isn't a waste of time, because often if you make the problem smaller you can find the issue yourself. For example, does this code still break with just 5 rows? – Calum You Jul 16 '18 at 19:37
  • @CalumYou Thanks. I will take a look and revise accordingly. – JH P Jul 16 '18 at 19:48
  • I guess there is a problem with the environment - that you might have used objects which were defined previously without realising, or masked those accidentally by installing another package. – tjebo Jul 16 '18 at 20:50

1 Answers1

1

I had a similar issue which was resolved by converting columns. Numeric columns were being imported as strings.

Sherman
  • 437
  • 6
  • 9