geom_point removes fill argument

Hi,
My issue is that I want to create a geom_point plot with ggplot2, but it just doesnt react to fill or color argument.
The output:


So it just turns to black doesnt matter what color I add.
My code is:

df %>% head()
LOCATION TIME Fertility worldpop Lifeexp loc2
1 AUS 1960 3.45 10.275 70.9 mindenki más
2 AUS 1961 3.55 10.5082 71.2 mindenki más
3 AUS 1962 3.43 10.7005 71.0 mindenki más
4 AUS 1963 3.34 10.9069 71.1 mindenki más
5 AUS 1964 3.15 11.1216 70.7 mindenki más
6 AUS 1965 2.97 11.3409 71.0 mindenki más

ggplot(data=df) +
geom_point( aes(x = Lifeexp, y=Fertility, size = as.numeric(worldpop),
fill=loc2),alpha=0.7) +
scale_size(range = c(4, 20)) +
scale_fill_manual(values = c("mindenki más"="green", "Magyarország"="red")) +
labs(x = "Születéskori várható életartam", y = "Termékenységi ráta") +
theme(
axis.title = element_text(
size=16
),

  • axis.text =  element_text(
    
  •   size=12
    
  • )
    
  • )
    Warning message:
    Removed 632 rows containing missing values (geom_point).

Can you turn this into a proper REPRoducible EXample (reprex) including sample data on a copy/paste friendly format?.

2 Likes

Hi @Marcell,
You are using TWO functions to fill the point symbols: fill=loc2 and scale_fill_manual(......).
Comment-out the second one and I think it will work, or replace with colour=loc2.
HTH

df=data.frame(
  TIME = c(1960, 1961, 1962, 1963, 1964, 1960),
  Fertility = c(3.45, 3.55, 3.43, 3.34, 3.15, 2.02),
  worldpop = c(10.275, 10.5082, 10.7005, 10.9069, 11.1216, 9.983514),
  Lifeexp = c(70.9, 71.2, 71, 71.1, 70.7, 68.1),
  LOCATION = as.factor(c("AUS", "AUS", "AUS", "AUS", "AUS", "HUN")),
  loc2 = as.factor(c("mindenki más",
                     "mindenki más","mindenki más","mindenki más",
                     "mindenki más","Magyarország"))
)
library(ggplot2)

ggplot(data=df) +
  geom_point( aes(x = Lifeexp, y=Fertility, size = as.numeric(worldpop),
                  fill=loc2),alpha=0.7) +
  scale_size(range = c(4, 20)) +
  scale_fill_manual(values = c("mindenki más"="#52AAA7", "Magyarország"="red")) +
  labs(x = "Születéskori várható életartam", y = "Termékenységi ráta") +
  theme(
    axis.title = element_text(
      size=16
    ),
    axis.text =  element_text(
      size=12
    )
  )

I used the color aesthetic and scale_color_manual()

df=data.frame(
  TIME = c(1960, 1961, 1962, 1963, 1964, 1960),
  Fertility = c(3.45, 3.55, 3.43, 3.34, 3.15, 2.02),
  worldpop = c(10.275, 10.5082, 10.7005, 10.9069, 11.1216, 9.983514),
  Lifeexp = c(70.9, 71.2, 71, 71.1, 70.7, 68.1),
  LOCATION = as.factor(c("AUS", "AUS", "AUS", "AUS", "AUS", "HUN")),
  loc2 = as.factor(c("mindenki más",
                     "mindenki más","mindenki más","mindenki más",
                     "mindenki más","Magyarország"))
)
library(ggplot2)

ggplot(data=df) +
  geom_point( aes(x = Lifeexp, y=Fertility, size = as.numeric(worldpop),
                  color=loc2),alpha=0.7) +
  scale_size(range = c(4, 20)) +
  scale_color_manual(values = c("mindenki más"="#52AAA7", "Magyarország"="red")) +
  labs(x = "Születéskori várható életartam", y = "Termékenységi ráta") +
  theme(
    axis.title = element_text(
      size=16
    ),
    axis.text =  element_text(
      size=12
    )
  )

Created on 2020-04-26 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.