当前,我想在ggplot中进行绘制,其中线条的大小和颜色根据完成的geom_smooth进行更改。我一直试图在geom_smooth aes选项中添加它,但它向我显示了两个图例:一个用于颜色,一个用于线的大小。你能帮我吗?
ggplot(data = subset(data.est,!(l %in% c(1,2))),
aes(x = sim, y = Error, color = factor(l), size = factor(l))) +
geom_smooth(method = 'loess',formula = y ~ x, se = F) +
geom_hline(yintercept = 0,col = 'black') +
labs(color = "Needle length", x = "Number of simulations" , y = "Squared error") +
scale_color_manual(values =
c('red','red','red','darkred','red','red','red','red','red')) +
scale_alpha_manual(values = seq(0.1,1,length.out = 9)) +
scale_color_brewer(type = 'div',direction = -1, palette = 'BrBG') +
xlim(500,1000)
structure(list(l = c(1L, 11L, 10L, 9L, 2L, 11L, 7L, 3L, 10L,
4L, 10L, 5L, 6L, 8L, 9L, 9L, 11L, 2L, 7L, 2L, 2L, 1L, 3L, 10L,
5L, 3L, 6L, 8L, 10L, 10L, 11L, 11L, 8L, 1L, 9L, 4L, 8L, 1L, 2L,
5L, 7L, 11L, 2L, 6L, 3L, 8L, 7L, 6L, 11L, 8L, 4L, 6L, 1L, 4L,
1L, 3L, 10L, 2L, 1L, 1L, 4L, 2L, 9L, 1L, 8L, 3L, 1L, 4L, 3L,
11L, 9L, 9L, 10L, 5L, 10L, 1L, 8L, 1L, 5L, 1L, 10L, 3L, 5L, 7L,
5L, 4L, 6L, 3L, 9L, 5L, 4L, 11L, 5L, 11L, 10L, 8L, 7L, 3L, 3L,
4L), sim = c(107L, 829L, 932L, 513L, 507L, 330L, 930L, 135L,
394L, 654L, 718L, 652L, 983L, 616L, 500L, 990L, 549L, 396L, 463L,
822L, 325L, 931L, 589L, 270L, 565L, 506L, 447L, 430L, 747L, 414L,
424L, 220L, 954L, 618L, 171L, 947L, 323L, 197L, 480L, 402L, 692L,
932L, 815L, 663L, 329L, 521L, 415L, 668L, 847L, 927L, 337L, 33L,
322L, 449L, 955L, 922L, 827L, 998L, 971L, 508L, 613L, 550L, 772L,
275L, 632L, 85L, 155L, 170L, 953L, 485L, 374L, 854L, 261L, 42L,
428L, 131L, 597L, 338L, 329L, 180L, 476L, 678L, 624L, 714L, 910L,
360L, 800L, 49L, 68L, 223L, 524L, 925L, 527L, 491L, 441L, 780L,
764L, 551L, 436L, 349L), Error = c(0.0101670093068795, 0.0333634451103134,
0.00110520504147119, 0.028301531087948, 0.000298800647667255,
0.0635776657638243, 0.00663599930005768, 1.47196599261218, 0.256391067242013,
0.038277634211877, 0.0177993359544238, 0.00296713793566502, 0.00249657996732793,
0.000844461474275876, 0.0480599904421951, 0.002899124518668,
0.0858530902932876, 0.0590600477951372, 0.0190356018221132, 0.0581404919987103,
0.281667678212426, 0.053636592440469, 0.0278373137029251, 0.00630124778723973,
0.00114121491039577, 0.0351017891695027, 0.000708096101754807,
0.00215314776811059, 0.134540995133445, 0.14173321282681, 0.143490357497459,
0.00354219603061888, 0.0130854224685769, 0.000415366099997319,
0.192314026136183, 0.00383382099588482, 0.000282149419654538,
0.503381577333123, 0.287905504025059, 0.0367644882685149, 0.0687261770900463,
2.13743806184716e-05, 0.00296869998059188, 0.000532924183125649,
0.0220247404685192, 0.00641189675448479, 0.0321901760394775,
2.96199229171648e-05, 0.00155015921826204, 0.0848423674016669,
0.205276061325583, 0.0739989260447227, 0.0579325198124508, 0.0428291844070546,
0.0438000184287992, 0.00615749852654801, 0.0591407415396722,
0.00106004847789361, 0.000999189621100586, 0.00394443973791914,
0.0218145986793207, 0.0106595516078209, 0.0459275847313977, 0.147493152075202,
0.186223913430443, 0.0443347433037691, 2.41919774707321, 0.00931274881623056,
0.000289084654480865, 0.0176630565524408, 0.000940778203425078,
0.25244328472803, 0.196366861971049, 0.00962643405885916, 0.050162785770824,
0.247303545642654, 0.0156407722998984, 0.143823493172342, 0.0261210883420279,
0.283979214118036, 0.0475375375754916, 0.000709244540739497,
0.000773963444577904, 0.0441217545092607, 0.00199646827474003,
0.0284772962285157, 0.000202375856502037, 1.72384505753732, 0.0560095299611507,
0.000336478631196564, 0.000508278582507821, 0.0453971717148919,
0.000725518099728713, 0.00333759994051756, 0.169116880951135,
0.125565021090312, 0.00054035813261054, 0.00212401485152619,
0.0382776342118771, 0.0530786410214522)), row.names = c(98L,
10830L, 9932L, 8512L, 1499L, 10331L, 6927L, 2128L, 9394L, 3648L,
9718L, 4647L, 5979L, 7614L, 8499L, 8989L, 10550L, 1388L, 6460L,
1814L, 1317L, 922L, 2582L, 9270L, 4560L, 2499L, 5443L, 7428L,
9747L, 9414L, 10425L, 10221L, 7952L, 609L, 8170L, 3941L, 7321L,
188L, 1472L, 4397L, 6689L, 10933L, 1807L, 5659L, 2322L, 7519L,
6412L, 5664L, 10848L, 7925L, 3331L, 5029L, 313L, 3443L, 946L,
2915L, 9827L, 1990L, 962L, 499L, 3607L, 1542L, 8771L, 266L, 7630L,
2078L, 146L, 3164L, 2946L, 10486L, 8373L, 8853L, 9261L, 4037L,
9428L, 122L, 7595L, 329L, 4324L, 171L, 9476L, 2671L, 4619L, 6711L,
4905L, 3354L, 5796L, 2042L, 8067L, 4218L, 3518L, 10926L, 4522L,
10492L, 9441L, 7778L, 6761L, 2544L, 2429L, 3343L), class = "data.frame")
这是我的样本的样子。因数l是1到11的整数,但是在ggplot中,我只想从l> 3
答案 0 :(得分:1)
ggplot(data = subset(data.est,!(l %in% c(1,2))),
aes(x = sim, y = Error, color = factor(l), size = factor(l))) +
geom_smooth(method = 'loess',formula = y ~ x, se = F) +
geom_hline(yintercept = 0,col = 'black') +
labs(x = "Number of simulations" , y = "Squared error") +
# can only have one color scale
# scale_color_manual(values = c('red','red','red','darkred','red','red','red','red','red')) +
guides(color = guide_legend(title = "Needle length"), size = guide_legend(title = "Needle length")) +
# alpha not used
#scale_alpha_manual(values = seq(0.1,1,length.out = 9)) +
scale_color_brewer(type = 'div',direction = -1, palette = 'BrBG') +
xlim(500,1000)
如果仅命名其中一个,则图例不会合并。如果您给它们使用相同的名称(两者都命名或两者都不命名),则会将它们组合在一起。