0
votes

I'm using ggplot2 in R and have been having trouble with a scatter plot using geom_point. I have the basic x, y scatter plot but attempted to color points according to another variable: EVENT. EVENT is either "wet" or "dry". I tried changing EVENT to a factor since it's just two levels, but that didn't change anything. The plot seems to be shrunk horizontally. I can get a "normal" plot if I switch EVENT with a continuous variable, but get a shrunken plot when I enter factors or categorical. Here is my code and plot.

ggplot(data=mydata, aes(x=conversion.ratio, y=sampler.purchaser.ratio)) +
  geom_point(alpha=.5, aes(color=EVENT.WET.DRY))` 

resulting plot

Any ideas? Am I missing something? I tried making a reproducible example:

EVENT.WET.DRY<-c(rep("Dry",7),rep("Wet",18))
conversion.ratio=rnorm(25,.5,.1)
sampler.purchaser.ratio=rnorm(25,.7,.05)
mydata<-data.frame(factor(EVENT.WET.DRY), conversion.ratio, sampler.purchaser.ratio)

ggplot(data=mydata, aes(x=conversion.ratio, y=sampler.purchaser.ratio)) +
  geom_point(aes(color=EVENT.WET.DRY))

This plots fine, however. What it is is something to do with the variable EVENT.WET.DRY within the original data set. I've already had some issues with it because it was originally a SAS file that was transferred to an SPSS file that I am bringing into R.

1
What do you mean by "shrunk"? Is the x axis supposed to include larger values? The size of the image file itself depends on things that happen after you run the ggplot code itself. Are you sure the other points aren't simply hidden due to over plotting? - joran
It would help if you provdided a reproducible example with some sample input data that we can use to test. Are you sure your colors are properly coded? What does table(mydata$EVENT.WET.DRY) return? Also, there is no need to respecify data= in the geom_point. It will inherit from the main ggplot call. - MrFlick
What I mean by "shrunk", is that when the plot shows up in my plot viewer I can't even see it because it is squashed together. I have added a snip of what I see after I create the plot as oppose to the image file itself. - J. Sweet
Your ggplot2 code should be fine, but it's hard to say without a reproducible example. Try resetting your current graphical device with dev.off() or restarting R/Rstudio and try your code again. - DunderChief
What are the levels of the factor EVENT.WET.DRY? Could one of the levels have a bunch of blanks spaces in it so the word is actually really long? - aosmith

1 Answers

0
votes

So, here are the levels of EVENT.WET.DRY:

levels(viniq$EVENT.WET.DRY)
[1] "Dry                                                                                                                                                                                                                                                            "
[2] "Wet                                                                                                                                                                                                                                                            "

Hence, @aosmith you were correct. There are a number of spaces after each level.

levels(mydata$EVENT.WET.DRY) <- list("Dry"="Dry                                                                                                                                                                                                                                                            ", "Wet"="Wet                                                                                                                                                                                                                                                            ")

This did the trick! After searching, I found a better way to do this:

trim.trailing <- function (x) sub("\\s+$", "", x)
mydata$EVENT.WET.DRY <- trim.trailing(mydata$EVENT.WET.DRY)