D Pinto
D Pinto

Reputation: 901

Error with legend of ggplot graph

I have a data set with three variables: Index of Economic Freedom, Total GDP and GDP per capita of ten countries.

I am trying to plot a graph, in which the size of the circles should be proportional to the GDP of each country.

However, on the right side of the graph, I am getting these blue squares instead of circles with different sizes.

How can I get rid of these blue squares and have R to display the circles with different sizes instead?

Here is my result: enter image description here

Here is the data and the code I am using:

score <- c(60,65.7,48.9,47.9,44.1,67.1,81.4,71.2,61.0,68.7)  

gdp <- c(35.146, 26.499,285.541,130.066,777.945, 20.352,999.595,361.402,102.811,11.411)

gdp.percapita <- c(1150.36,9506.12,7534.06,6247.28,18749.34,6190.75,43073.06,42596.55,11044.25,32036.09)

x <- data.frame(score, gdp, gdp.percapita)

p <- ggplot(data=x,aes(x= score, y= gdp.percapita, size = gdp))
p <- p + theme(axis.title.x = element_text(face="bold", size=20)) +
labs(x="Economic Index Freedom")
p <- p + theme(axis.title.y = element_text(face="bold", size=20)) +
labs(y="GDP per Capita")                                               
p + geom_point(shape = 1) +
  scale_size_area(max_size=25) + 
  stat_smooth(method = lm)

Upvotes: 2

Views: 177

Answers (1)

Axeman
Axeman

Reputation: 35297

The size argument in the ggplot call is passed down to both geom_point as well as stat_smooth. The problem is in the latter, where it is taken as the line width (I suppose). Moving the size into geom_point resolves the issue.

ggplot(data = x, aes(x = score, y = gdp.percapita)) +                                       
  geom_point(shape = 1, aes(size = gdp)) +
  scale_size_area(max_size = 25) +
  stat_smooth(method = lm) +
  xlim(42, 84) +
  xlab('economic index freedom') +
  ylab('GDP per capita') +
  theme_bw()

enter image description here

Upvotes: 3

Related Questions