我试图根据连续变量的值为一些错误栏着色。它会产生错误:
错误:提供给连续刻度的离散值
以下是数据样本:
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"))
ggplot代码:
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")
此外,一旦它成为功能我想使用scale_color_gradient()作为errobars,但我不确定如何指定错误栏的刻度颜色与点的颜色分开。
答案 0 :(得分:1)
正如@ epi10所建议的那样,您试图将color
映射到两个不同的变量。 Shape 21
是一个空心圆,接受fill
映射,这意味着您可以将fill
用于其中,color
用于另一个。然后可以使用scale_fill_manual
(对于离散)和scale_colour_gradient
(对于连续)手动调整其颜色。
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)