简体   繁体   中英

Issues with plot using geom_point in ggplot2 in R

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))` 

结果图

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM