user114875
user114875

Reputation:

shapes coming out filled and not hollow when using ggplot R

I have a problem I am using ggplot to make 3 scatter plots acording to 3 diffrnet factors in my data(years 1997,2002,2007) this is the code

 countries_data <- read.delim("D:/Rdirectory/countries_data.txt")  #reading the file
countries_data<-subset(countries_data,continent!='Oceania') # taking oceania out
countries_data<-subset(countries_data,year==1997 | year ==2002 | year==2007)

p <- ggplot(countries_data, aes(x =gdpPercap, y = lifeExp))
p<- p + geom_point() + scale_x_log10()
p<-p + geom_point() + labs(x="GDP Per Capita",y="Life Expectancy")
p<-p + geom_point() +facet_wrap(~ year)

p+geom_point()

p<-p+geom_point(aes(shape = continent))+scale_shape_manual(values =c(0,1,2,3))
p<-p+ggtitle('Life Expectancy vs. GDP by continent, 1997-2007')

p+geom_point()

problem is the shapes ae coming out filled instead of hollow even though when you look to the right side it seems R is reconizing hollow shapes

enter image description here

any suggestions?

Upvotes: 2

Views: 2251

Answers (1)

eipi10
eipi10

Reputation: 93881

I think what's happening is that you're first plotting the points without a shape aesthetic, so all the points are filled circles. Then you're adding a shape aesthetic (continent), which overplots unfilled markers (as chosen with scale_shape_manual) on top of the filled circles, making the markers look filled. The legend markers are unfilled because no legend is generated by all those calls to geom_point without the shape aesthetic.

To fix this, you should have only one call to geom_point: geom_point(aes(shape = continent)) or just geom_point() if you put shape=continent in your first aes() statement. In other words, you can create your plot as follows:

p <- ggplot(countries_data, 
            aes(x = gdpPercap, y = lifeExp, shape=contintent)) +
  geom_point() + 
  scale_x_log10() + 
  labs(x="GDP Per Capita",y="Life Expectancy") +
  facet_wrap(~ year) +
  ggtitle('Life Expectancy vs. GDP by continent, 1997-2007')

Here's an example with a built-in data frame:

# Filled circles
p1 = ggplot(mtcars, aes(wt, mpg)) +
  geom_point() 

# Unfilled markers plotted over filled circles
p2 = p1 + geom_point(aes(shape = factor(cyl))) +
  scale_shape_manual(values=0:2)

# Only unfilled markers plotted
p3 = ggplot(mtcars, aes(wt, mpg, shape=factor(cyl))) +
  geom_point() +
  scale_shape_manual(values=0:2)

enter image description here

Also, for future reference, you only need to call geom_point once. Multiple calls (using the same data and aesthetics) just overplot the same thing over and over again. You also don't need to resave the plot object after each line. You can instead just chain each line together. Thus, the code below...

p <- ggplot(countries_data, aes(x =gdpPercap, y = lifeExp))
p <- p + geom_point() + scale_x_log10()
p <- p + geom_point() + labs(x="GDP Per Capita",y="Life Expectancy")
p <- p + geom_point() + facet_wrap(~ year)

...can be changed to this:

p <- ggplot(countries_data, aes(x =gdpPercap, y = lifeExp)) +
  geom_point() + 
  scale_x_log10() + 
  labs(x="GDP Per Capita",y="Life Expectancy") +
  facet_wrap(~ year)

Upvotes: 4

Related Questions