ggplot在尝试更改点颜色和/或形状时删除数据

时间:2018-03-23 20:19:42

标签: r ggplot2

我正在尝试用置信区间图创建一个简单的点估计。我可以按照我的意愿绘制它,直到我尝试改变点形状和/或颜色。当我尝试更改时,我得到“警告:删除了包含缺失值的4行(geom_point)”。并最终得出一个空白的情节。

我已经检查并尝试了以下建议: here here here and here  和其他几个地方但无济于事。

可重复的例子

library(ggplot2)
set.seed(1)

# Create some sample data 

point_est <- 4:1
se        <- runif(4)

df <- data.frame(point_est = point_est,
                 se        = se,
                 lower     = point_est - se,
                 upper     = point_est + se,
                 year      = c("c", "c", "p", "p"),
                 group     = letters[1:4])

group_names <- paste0("Display Name for \n Group ", LETTERS[1:4])
names(group_names) <- letters[1:4]

legend_text <- c("Previous Year Rate with 95% Confidence Intervals",
                 "Current Year Rate with 95% Confidence Intervals")
names(legend_text) <- c("p", "c")

df$year = factor(df$year, levels = names(legend_text), labels = legend_text)
df$group = factor(df$group, levels = names(group_names), labels = group_names)

# Plot looks good except the colors and shape of the points need changing
ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
  geom_point(size = 3.2) +
  scale_x_discrete(drop=FALSE) +
  scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
  labs(x = NULL,
       y = "This is my Left Axis") +
  theme(legend.title = element_blank(),
        legend.position = "bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "black"),
        panel.border = element_rect(colour = "black", fill=NA),
        panel.background = element_blank()) 

# now change the shapes of the points and the colors of the error bars
shapes <- c(17, 15)
names(shapes) <- names(legend_text)

colors <- c("pink", "blue")
names(colors) <- names(legend_text)

ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
  geom_point(size = 3.2) +
  scale_x_discrete(drop=FALSE) +
  scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
  scale_shape_manual(values = shapes) +
  scale_color_manual(values = colors) +
  labs(x = NULL,
       y = "This is my Left Axis") +
  theme(legend.title = element_blank(),
        legend.position = "bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "black"),
        panel.border = element_rect(colour = "black", fill=NA),
        panel.background = element_blank()) 
#> Warning: Removed 4 rows containing missing values (geom_point).

# Blank plot now and warnings:(

2 个答案:

答案 0 :(得分:1)

如果将矢量直接放入ggplot中,它将起作用。 对于scale_shape_manual,为值设置c(17,15),为scale_color_manual设置值c(“Pink”,“Blue”)。或者只是不为形状和颜色矢量指定名称。这就是把它扔掉的原因。

ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
  geom_point(size = 3.2) +
  scale_x_discrete(drop=FALSE) +
  scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
  scale_shape_manual(values = c(17, 15)) +
  scale_color_manual(values = c("pink", "blue")) +
  labs(x = NULL,
       y = "This is my Left Axis") +
  theme(legend.title = element_blank(),
        legend.position = "bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "black"),
        panel.border = element_rect(colour = "black", fill=NA),
        panel.background = element_blank()) 



######if you want to use the vectors do not name them 
shapes <- c(17, 15)

colors <- c("pink", "blue")

ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
  geom_point(size = 3.2) +
  scale_x_discrete(drop=FALSE) +
  scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
  scale_shape_manual(values = shapes) +
  scale_color_manual(values = colors) +
  labs(x = NULL,
       y = "This is my Left Axis") +
  theme(legend.title = element_blank(),
        legend.position = "bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "black"),
        panel.border = element_rect(colour = "black", fill=NA),
        panel.background = element_blank()) 

答案 1 :(得分:1)

这种情况正在发生,因为您使用names(legend_text)而不是legend_text作为shapescolors向量的名称。 legend_text与您数据的year列中的值相匹配。对names(colors) <- legend_text执行shapes和同样的操作,情节也会奏效。没有绘制任何内容,因为colorsshapes向量的名称与df$year的任何级别都不匹配,因此没有为year中的实际值分配颜色或形状}。

看起来好像你在levels函数中被labelsfactor绊倒了。默认情况下,levels是数据中现有的唯一值集合,labels设置为等于级别。但是,如果在labels中包含factor参数,则数据值会重新标记为labels参数中的值。

为了具体化,请在下面的代码中注明shapescolors向量的名称为pc,这与以下值不同df$year

> df[ , "year", drop=FALSE]
                                              year
1  Current Year Rate with 95% Confidence Intervals
2  Current Year Rate with 95% Confidence Intervals
3 Previous Year Rate with 95% Confidence Intervals
4 Previous Year Rate with 95% Confidence Intervals

> levels(df$year)
[1] "Previous Year Rate with 95% Confidence Intervals" "Current Year Rate with 95% Confidence Intervals"


> shapes
 p  c 
17 15 
> colors
     p      c 
"pink" "blue"