ggplot2图例(指南)格式化(美观的间距或分组)

时间:2016-06-03 17:39:16

标签: r ggplot2 format legend

我想做的是改善这个ggplot的修道人的传说。我试图绘制多个图,然后将它们粘在一起,但它并没有真正起作用。我想知道是否有人有任何关于如何分隔传说的想法,如下图所示。我已经放了我目前使用的代码,除了我将标题更改为伪造的代码,以使其在我的组织之外呈现出来。

任何人都有任何想法可以提高图例的清晰度,以便不同的群体对此图表的观看者更清晰?

代码:

MemberGroup <- structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
25L, 25L, 25L, 25L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 
49L, 49L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 
61L), .Label = c("Bogus1_Group1", 
"Bogus1_Group1", "Bogus1_Group1", 
"Bogus1_Group1", "Bogus1_Group1", 
"Bogus1_Group1", "Bogus1_Group1", 
"Bogus1_Group1", "Bogus1_Group1", 
"Bogus1_Group1", "Bogus1_Group1", 
"Bogus1_Group1", "Bogus2_Group1", 
"Bogus2_Group1", "Bogus2_Group1", 
"Bogus2_Group1", "Bogus2_Group1", 
"Bogus2_Group1", "Bogus2_Group1", 
"Bogus2_Group1", "Bogus2_Group1", 
"Bogus2_Group1", "Bogus2_Group1", 
"Bogus2_Group1", "Bogus3_Group1", 
"Bogus3_Group1", "Bogus3_Group1", 
"Bogus3_Group1", "Bogus3_Group1", 
"Bogus3_Group1", "Bogus3_Group1", 
"Bogus3_Group1", "Bogus3_Group1", 
"Bogus3_Group1", "Bogus3_Group1", 
"Bogus3_Group1", "Bogus4_Group2", 
"Bogus4_Group2", "Bogus4_Group2", 
"Bogus4_Group2", "Bogus4_Group2", 
"Bogus4_Group2", "Bogus4_Group2", 
"Bogus4_Group2", "Bogus4_Group2", 
"Bogus4_Group2", "Bogus4_Group2", 
"Bogus4_Group2", "Bogus5_Group2", 
"Bogus5_Group2", "Bogus5_Group2", 
"Bogus5_Group2", "Bogus5_Group2", 
"Bogus5_Group2", "Bogus5_Group2", 
"Bogus5_Group2", "Bogus5_Group2", 
"Bogus5_Group2", "Bogus5_Group2", 
"Bogus5_Group2", "Bogus6_Group3", 
"Bogus6_Group3", "Bogus6_Group3", 
"Bogus6_Group3", "Bogus6_Group3", 
"Bogus6_Group3", "Bogus6_Group3", 
"Bogus6_Group3", "Bogus6_Group3", 
"Bogus6_Group3", "Bogus6_Group3", 
"Bogus6_Group3"), class = "factor");

Display <- c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L);

Oid <- c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
        3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
        4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
        5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L);

AgeBucket <- structure(c(1L, 7L, 13L, 19L, 25L, 31L, 37L, 
43L, 49L, 55L, 61L, 67L, 1L, 7L, 13L, 19L, 25L, 31L, 37L, 
43L, 49L, 55L, 61L, 67L, 1L, 7L, 13L, 19L, 25L, 31L, 37L, 
43L, 49L, 55L, 61L, 67L, 1L, 7L, 13L, 19L, 25L, 31L, 37L, 
43L, 49L, 55L, 61L, 67L, 1L, 7L, 13L, 19L, 25L, 31L, 37L, 
43L, 49L, 55L, 61L, 67L, 1L, 7L, 13L, 19L, 25L, 31L, 37L, 
43L, 49L, 55L, 61L, 67L), .Label = c("10-14", "10-14", "10-14", 
"10-14", "10-14", "10-14", "15-19", "15-19", "15-19", "15-19", 
"15-19", "15-19", "20-24", "20-24", "20-24", "20-24", "20-24", 
"20-24", "25-29", "25-29", "25-29", "25-29", "25-29", "25-29", 
"30-34", "30-34", "30-34", "30-34", "30-34", "30-34", "35-39", 
"35-39", "35-39", "35-39", "35-39", "35-39", "40-44", "40-44", 
"40-44", "40-44", "40-44", "40-44", "45-49", "45-49", "45-49", 
"45-49", "45-49", "45-49", "50-54", "50-54", "50-54", "50-54", 
"50-54", "50-54", "55-59", "55-59", "55-59", "55-59", "55-59", 
"55-59", "60-64", "60-64", "60-64", "60-64", "60-64", "60-64", 
"65-69", "65-69", "65-69", "65-69", "65-69", "65-69"), class = "factor");

BinMin <- c(10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 
60L, 65L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 
60L, 65L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 
60L, 65L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 
60L, 65L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 
60L, 65L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 
60L, 65L);

per <- c(0.387832699619772, 0.317991631799163, 
0.123521681997372, 0.0947368421052632, 0.0972359328726555, 
0.0937873357228196, 0.103455162772119, 0.108816521048451, 
0.114578206516389, 0.116530727610434, 0.12681131040965, 0.110502997133177, 
0.20532319391635, 0.259414225941423, 0.258869908015769, 0.232456140350877, 
0.16436327739388, 0.0949820788530466, 0.0728979428799681, 
0.0557320624834525, 0.0484299635790924, 0.0429621254946297, 
0.0460562387405029, 0.0406567630961689, 0.0342205323193916, 
0.0502092050209205, 0.0801576872536137, 0.171929824561404, 
0.262092793682132, 0.328853046594982, 0.387257839025364, 
0.42176330420969, 0.450241165469042, 0.462731163692159, 0.479439179133704, 
0.445660672400313, 0.0418250950570342, 0.0167364016736402, 
0.00919842312746386, 0.00701754385964912, 0.00740375123395854, 
0.00627240143369176, 0.00279608548032754, 0.00238284352660842, 
0.0016733930504971, 0.00121133812484858, 0.00109657711286912, 
0.000260620276257493, 0.102661596958175, 0.0711297071129707, 
0.0315374507227332, 0.0254385964912281, 0.0108588351431392, 
0.00686977299880526, 0.00499300978629918, 0.00357426528991263, 
0.00246087213308397, 0.00185738512476783, 0.00117490404950262, 
0.00130310138128746, 0.608365019011407, 0.54602510460251, 
0.314060446780552, 0.292982456140351, 0.285291214215202, 
0.253584229390681, 0.226682644297983, 0.231665342864707, 
0.237621813170588, 0.246305418719212, 0.265058353567792, 
0.252541047693511);

Ct <- c(102L, 152L, 94L, 108L, 197L, 
          314L, 518L, 822L, 1164L, 1443L, 1619L, 424L, 54L, 124L, 197L, 
          265L, 333L, 318L, 365L, 421L, 492L, 532L, 588L, 156L, 9L, 
          24L, 61L, 196L, 531L, 1101L, 1939L, 3186L, 4574L, 5730L, 
          6121L, 1710L, 11L, 8L, 7L, 8L, 15L, 21L, 14L, 18L, 17L, 15L, 
          14L, 1L, 27L, 34L, 24L, 29L, 22L, 23L, 25L, 27L, 25L, 23L, 
          15L, 5L, 160L, 261L, 239L, 334L, 578L, 849L, 1135L, 1750L, 
          2414L, 3050L, 3384L, 969L);

InRange <- c(263L, 478L, 761L, 
               1140L, 2026L, 3348L, 5007L, 7554L, 10159L, 12383L, 12767L, 
               3837L, 263L, 478L, 761L, 1140L, 2026L, 3348L, 5007L, 7554L, 
               10159L, 12383L, 12767L, 3837L, 263L, 478L, 761L, 1140L, 2026L, 
               3348L, 5007L, 7554L, 10159L, 12383L, 12767L, 3837L, 263L, 
               478L, 761L, 1140L, 2026L, 3348L, 5007L, 7554L, 10159L, 12383L, 
               12767L, 3837L, 263L, 478L, 761L, 1140L, 2026L, 3348L, 5007L, 
               7554L, 10159L, 12383L, 12767L, 3837L, 263L, 478L, 761L, 1140L, 
               2026L, 3348L, 5007L, 7554L, 10159L, 12383L, 12767L, 3837L);


df <- as.data.frame(list(MemberGroup,Display,Oid,AgeBucket,BinMin,per,Ct,InRange))

library(ggplot2)

p <- ggplot(data=df, aes(x=BinMin, y=per, color = MemberGroup))#, order = as.numeric(Oid))
p <- p + geom_line(aes(linetype=MemberGroup, color=MemberGroup, size=1))
p <- p + theme(legend.position=c(.4,.8)) + guides(size = "none")
p <- p + theme(text = element_text(size = 30))
p <- p + scale_y_continuous(labels = scales::percent, breaks=seq(0, .80, .05))
p <- p + scale_x_continuous(breaks=seq(10,65,5), labels = unique(all_labs) )
p <- p + theme(axis.text.x=element_text(angle=50,vjust=0.5))
p <- p + scale_color_manual(values=c("#ff1919", "#ff4c4c", "#ff9999","#76ee00","#32cd32","#1874cd"))
p <- p + scale_linetype_manual(values = c("dashed","dashed","dashed","dotted","dotted","dotted"))
p <- p + xlab("XXX Range")
p <- p + ylab("%")
p <- p + theme(legend.background = element_rect(fill = "transparent"),legend.key = element_rect(fill = "transparent",color ="transparent"))
p <- p + guides(color = guide_legend(override.aes = list(size=1)))
p <- p + theme(legend.key.width = unit(3,"cm"))
p <- p + theme(legend.title=element_blank())
p

enter image description here

1 个答案:

答案 0 :(得分:2)

您可能需要深入研究图例的结构。

(您的代码存在一些问题。例如,请检查以下行:scale_x_continuous(.....geom_line(....

获得情节p后,试试这个:

library(gtable)
library(grid)
# Get the ggplot grob
g = ggplotGrob(p)

# Get the legend
leg = g$grobs[[which(g$layout$name == "guide-box")]]$grobs[[1]]

# Add rown to the legend gtable
leg = gtable_add_rows(leg, unit(1, "lines"), pos = 8) # Space below "Group 2"
leg = gtable_add_rows(leg, unit(1, "lines"), pos = 6) # Space below "Group 1"

# Put the legned back into the plot
g$grobs[[which(g$layout$name == "guide-box")]]$grobs[[1]] = leg

# Draw it
grid.newpage()
grid.draw(g)