0

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.

J. Sweet
  • 15
  • 8
  • 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 Oct 21 '15 at 21:41
  • 2
    It would help if you provdided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-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 Oct 21 '15 at 22:04
  • 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 Oct 21 '15 at 22:42
  • 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 Oct 21 '15 at 22:55
  • 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 Oct 22 '15 at 00:00
  • I agree, this is going to be impossible to solve without a reproducible example, because nothing you've shown us in the question would plausibly produce the behavior you're describing. – joran Oct 22 '15 at 01:01
  • Reinstall R and RStudio (i.e. delete/uninstall both and re-install). Your reproducible example plots perfectly on my system. – hrbrmstr Oct 22 '15 at 01:05

1 Answers1

0

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)
J. Sweet
  • 15
  • 8
  • Thank you everyone for trying to help. I know that without a reproducible example it was almost impossible, but I couldn't recreate it! – J. Sweet Oct 22 '15 at 16:39