jmbejara / comp-econ-sp19

Main Course Repository for Computational Methods in Economics (Econ 21410, Spring 2019)
48 stars 26 forks source link

Problem with HW 5 Visualization, Q14 #43

Closed henryli78 closed 5 years ago

henryli78 commented 5 years ago

I'm trying to understand plotnine's way of grouping and coloring points. I'm able to replicate Q14's intended diagram up to the legend, where whenever I try to add color = 'class' I get the error

ValueError: b'There are other near singularities as well. 0.65044\n'

Any ideas? My exact code is:

(plotnine.ggplot(data = mpg, mapping = plotnine.aes(x = 'displ', y = 'hwy', color = 'class')) + plotnine.geoms.geom_point() + plotnine.geom_smooth(method = 'loess'))

jmbejara commented 5 years ago

With this one, geom_smooth is trying to apply a smoother to the colors. This isn't going to work, though. When you write ggplot(data=mpg) + aes(x='displ', y='hwy', color = 'class') + ..., it is applying an aesthetic globally to the plot. Instead, you want geom_point and geom_smooth to each use different aesthetics. You can do this by removing the global aes and instead proving an aesthetic to each individually using the mapping keyword argument. For example, ... + geom_point(mapping=aes(x='displ', y='hwy', color = 'class')) + ...