2017-05-03 231 views
1

我试图颜色基于连续变量的值一些errorbars。它产生一个错误:GGPLOT2 geom_errorbar不接受连续的颜色规模

Error: Discrete value supplied to continuous scale

这里是数据的样本:

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,但我“不知道如何从这些点的单独指定刻度颜色为errorbars。

+2

你必须映射到颜色两个不同的变量('type' - 离散和'pop.under6yrs' - 连续)。 ggplot只允许你将一组数值映射到特定的美学。 – MrFlick

+2

你可以使用'填充点标记通过这样做,说,'geom_point(AES(填写=型),大小= 2,PCH = 21)geom_point''(和主摆脱'颜色= type'的ggplot调用),但是你会有两种不同的颜色映射,这会让人困惑。 – eipi10

回答

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) 

enter image description here

+0

谢谢大家。这是一个可以通过的解决方案。我很好奇:如果我在geom调用中为颜色设置美学而不是全局ggplot调用,那么不应该单独考虑它们,因为它们仅包含在该geom中? (或者即使我在全球范围内设置一个,在一个几何体中存在不同的美学调用应该覆盖它)。 – jzadra