Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot在尝试更改点颜色和/或形状时删除数据_R_Ggplot2 - Fatal编程技术网

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

R ggplot在尝试更改点颜色和/或形状时删除数据,r,ggplot2,R,Ggplot2,我试图用置信区间图创建一个简单的点估计。在我尝试改变点的形状和/或颜色之前,我可以让它按照我的意愿进行绘图。当我尝试更改其中一个时,我会得到“警告:删除了包含缺少值的4行(geom_point)”,并以空白绘图结束 我已经检查并尝试了以下建议: 还有其他几个地方,但都没用 可复制的例子 库(ggplot2) 种子(1) #创建一些示例数据 点_est如果将向量直接放入ggplot,它将起作用。 对于比例\形状\手动输入c(17,15)作为值,对于比例\颜色\手动输入c(“粉色”、“蓝色”)作为

我试图用置信区间图创建一个简单的点估计。在我尝试改变点的形状和/或颜色之前,我可以让它按照我的意愿进行绘图。当我尝试更改其中一个时,我会得到“警告:删除了包含缺少值的4行(geom_point)”,并以空白绘图结束

我已经检查并尝试了以下建议: 还有其他几个地方,但都没用

可复制的例子

库(ggplot2)
种子(1)
#创建一些示例数据

点_est如果将向量直接放入ggplot,它将起作用。 对于比例\形状\手动输入c(17,15)作为值,对于比例\颜色\手动输入c(“粉色”、“蓝色”)作为值。或者只是不为形状和颜色向量指定名称。这就是它扔掉它的原因

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()) 
ggplot(df,aes(x=组,y=点,颜色=年,标签=年,形状=年))+
几何误差条(aes(ymin=较低,ymax=较高),宽度=0.3)+
几何点(尺寸=3.2)+
比例x离散(下降=假)+
缩放y连续(秒轴=秒轴(~*3,name=“这是我的右轴”))+
比例\形状\手册(值=c(17,15))+
比例\颜色\手册(值=c(“粉色”、“蓝色”))+
实验室(x=NULL,
y=“这是我的左轴”)+
主题(legend.title=element_blank(),
legend.position=“底部”,
legend.background=元素_blank(),
legend.box.background=元素(color=“黑色”),
panel.border=元素(color=“black”,fill=NA),
panel.background=element\u blank()
######如果要使用向量,请不要命名它们

形状发生这种情况是因为您使用了
名称(图例文本)
而不是
图例文本
作为
形状
颜色
向量的名称<代码>图例\u文本
与数据的
年份
列中的值相匹配。做
名字(颜色)和迈克谢谢你的帮助。这在我的实际数据上是有效的,但有一个例外。假设df现在只是当前年份数据的子集。除了图例仅包含当前年份外,绘图是正确的。我希望图例仍然显示这两个级别,并拥有所有4个组,即使其中2个组将为空。您可以将
drop=FALSE
添加到
scale\u***\ u手动调用中,就像
scale\ux\u离散调用一样。我是个白痴。我为x轴分类做了这些,但忘了这也不是为传奇做的。非常感谢你的帮助。
> 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"