0

My issue is that I want to do a scatter plot and size the circles to a variable in the data. While I can make this happen (the variable needs to be rescaled for some reason), the image of the circles looks odd (like there is some pixelation happening) and the size of the dots in the legend also ends up distorted.

Any idea what gives? Data and code are below.

df_t<-structure(
  list(
    x = c(
      0.271802325581395,
      0.28416149068323,
      0.300177619893428,
      0.273542600896861,
      0.277777777777778,
      0.295302013422819
    ),
    y = c(
      0.783430232558139,
      0.708074534161491,
      0.810834813499112,
      0.689088191330344,
      0.766937669376694,
      0.651006711409396
    ),
    comp = 1:6,
    mkt_share = c(
      0.259953161592506,
      0.241921204072598,
      0.404237288135593,
      0.276032315978456,
      0.283212302434857,
      0.125510940129839
    )
  ),
  .Names = c("x", "y", "comp", "mkt_share"),
  row.names = c("p2", "p3", "p4", "p5", "p6", "p7"),
  class = "data.frame"
)

ggplot(data=df_t, aes(x=x, y=y,  label=round(y,2), color = as.factor(comp)))+
  geom_point(size = df_t$mkt_share*50)+
  geom_text(size=4, color="white")+
  coord_cartesian(xlim = c(0, .4), ylim=c(.6,.9))
vashts85
  • 1,069
  • 3
  • 14
  • 28
  • 1
    Change `geom_point` to `geom_point(aes(size = mkt_share)) +`. In your original code, you set the size outside of a call to `aes`, and also reached outside the ggplot environment to the original data frame (by using `df_t$mkt_share` instead of just `mkt_share`). ggplot took this literally, so the size values were very small, since the market share values are small, hence the need for the scaling factor of 50. On the other hand, mapping `mkt_share` to size inside a call to `aes` provides a faithful mapping to the size aesthetic. – eipi10 Jan 23 '17 at 19:15
  • I tried doing what you suggested but this did not seem to work -- rather it created a second legend called `mkt_share` – vashts85 Jan 23 '17 at 19:48
  • 1
    Yes, that's right. `comp` is mapped to color. It is a discrete variable with 6 different colors and labels 1 through 6. `mkt_share` is a continuous variable mapped to size. You'll get a different legend for each. Your original code (which produces a plot that looks fine on my system) "hard-codes" the sizes of the circles, giving you a single legend for `comp` but with circle sizes equal to `mtk_share` * 50. So you get a single legend, but without a scale that tells you what size circle is mapped to a given value of `mkt_share`. – eipi10 Jan 23 '17 at 20:07

1 Answers1

0
+guides(colour = guide_legend(override.aes = list(size=5)))

Then play around with size.

Found here:

How to increase the size of points in legend of ggplot2?

Community
  • 1
  • 1
vashts85
  • 1,069
  • 3
  • 14
  • 28