ggplot2 geom_errorbar不接受连续色标

ggplot2 geom_errorbar不接受连续色标,r,ggplot2,R,Ggplot2,我试图根据连续变量的值给一些错误条上色。它会产生一个错误: 错误:离散值提供给连续刻度 以下是数据示例: popdata <- structure(list(state_name = structure(c(3L, 8L, 17L, 10L, 20L, 7L, 14L, 5L, 15L, 11L, 19L, 9L, 4L, 12L, 18L, 1L, 6L, 2L, 16L, 13L), .Label = c("38075", "16061", "17123", "01029", "4

我试图根据连续变量的值给一些错误条上色。它会产生一个错误:

错误:离散值提供给连续刻度

以下是数据示例:

popdata <- structure(list(state_name = structure(c(3L, 8L, 17L, 10L, 20L, 
7L, 14L, 5L, 15L, 11L, 19L, 9L, 4L, 12L, 18L, 1L, 6L, 2L, 16L, 
13L), .Label = c("38075", "16061", "17123", "01029", "42057", 
"48299", "19067", "37039", "22083", "21005", "47071", "16057", 
"39039", "36017", "29201", "20057", "27035", "48427", "48061", 
"39035"), class = "factor"), low = c(0.059837713753631, 0.12202164206081, 
0.0787897528614793, 0.114032453341537, 0.148341308923047, 0.12706787216107, 
0.12274383236656, 0.0748379645570958, 0.15221930873579, 0.183733602914917, 
0.235290802271188, 0.164149979506739, 0.107250926481166, 0.11777709831118, 
0.223030101604386, 0.0367333722034264, 0.0672672733525947, 0.0783039335836511, 
0.211986216346096, 0.129363157696044), high = c(0.09668764742318, 
0.158453141004957, 0.0952271927220508, 0.145718902550631, 0.153078926872484, 
0.167159039955223, 0.146691963769807, 0.110326114620514, 0.177443038767736, 
0.221222794006902, 0.243313847555343, 0.201423981526801, 0.14822761364434, 
0.143842795549539, 0.242206666862943, 0.0963491004746789, 0.100396455887465, 
0.152986449716942, 0.237907857617593, 0.155683357367624), pop.under6yrs = c(779, 
1365, 4482, 1698, 87596, 1169, 3092, 990, 3295, 1737, 43427, 
1623, 988, 2540, 7431, 228, 1039, 249, 3959, 2680), type = c("Empirical Bayes estimate", 
"Measured rate", "Measured rate", "Measured rate", "Measured rate", 
"Empirical Bayes estimate", "Measured rate", "Empirical Bayes estimate", 
"Measured rate", "Empirical Bayes estimate", "Empirical Bayes estimate", 
"Measured rate", "Measured rate", "Empirical Bayes estimate", 
"Empirical Bayes estimate", "Measured rate", "Measured rate", 
"Empirical Bayes estimate", "Empirical Bayes estimate", "Measured rate"
), rate = c(0.0772670083762154, 0.139880882476302, 0.0865854200764759, 
0.129427522078593, 0.150707738634344, 0.146552623015333, 0.134513369024505, 
0.0918203369486423, 0.164881354080928, 0.202157936735647, 0.239290959114104, 
0.183181736988013, 0.12691131498471, 0.130537078580802, 0.232550431594941, 
0.0558337214718211, 0.0819252242429744, 0.112969248262823, 0.224816181949421, 
0.142364418366184)), .Names = c("state_name", "low", "high", 
"pop.under6yrs", "type", "rate"), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

此外,一旦这成为功能性的,我想对错误条使用scale\u color\u gradient(),但我“我不知道如何分别指定错误条和点的比例颜色

正如@epi10所建议的,您试图将
color
映射到两个不同的变量
Shape 21
是一个接受
fill
映射的空心圆,这意味着您可以对其中一个使用
fill
,对另一个使用
color
。然后可以使用
scale\u fill\u manual
(离散)和
scale\u color\u gradient
(连续)手动调整其颜色

库(ggplot2)
科尔斯%
ggplot(aes(比率、州名称、填充=类型))+
geom_errorbarh(aes(最小值=低,最大值=高,颜色=6岁以下流行),尺寸=1)+
几何点(尺寸=3,形状=21)+
xlim(0,NA)+
实验室(x=”贫困率“,
y=NULL,title=“测量的比率、经验贝叶斯估计和可信区间”)+
刻度\填充\手动(值=cols)+
比例颜色梯度2(低=浅蓝色、中=蓝色、高=深蓝色、中点=45000)

您有两个不同的变量映射到颜色(
类型
——离散型和
pop.under6yrs
——连续型)。ggplot只允许您将一组值映射到特定的美学效果。您可以在
geom_point
中使用填充点标记,例如,
geom_point(aes(fill=type),size=2,pch=21)
(并在主ggplot调用中去掉
color=type
),但您将有两种不同的颜色映射,这会让人困惑。谢谢大家。这是一个可行的解决办法。我很好奇:如果我在GEOM调用中设置了颜色的颜色,而不是全局GPGOT调用,难道它们不应该单独考虑它们,因为它们只包含在GEOM中吗?(或者,即使我在全局范围内设置了一个,geom中不同美学调用的存在也会覆盖它)。
popdata %>% 
ggplot(aes(rate, state_name, color = type)) +
geom_errorbarh(aes(xmin = low, xmax = high, color = pop.under6yrs)) +
geom_point(size = 1) +
xlim(0, NA) +
labs(x = "Rate of poverty",
     y = NULL, title = "Measured Rates, Empirical Bayesian Estimates, and Credible Intervals")
library(ggplot2)

cols<-c("Empirical Bayes estimate"='red','Measured rate'='black')

popdata %>% 
  ggplot(aes(rate, state_name,fill=type)) +
  geom_errorbarh(aes(xmin = low, xmax = high, color = pop.under6yrs),size=1) +
  geom_point(size = 3,shape=21) +
  xlim(0, NA) +
  labs(x = "Rate of poverty",
       y = NULL, title = "Measured Rates, Empirical Bayesian Estimates, and Credible Intervals")+
  scale_fill_manual(values=cols)+
  scale_colour_gradient2(low='light blue',mid='blue',high='dark blue',midpoint=45000)