2013-09-21 4 views
5

ist hier meine Daten:Zwei Legenden für polare ggplot (mit einer maßgeschneiderten)

data <- structure(list(Indicator = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 
2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L, 
2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L), .Label = c("Causality", 
"Climatechangeriskperceptions", "Currentadaptationoptions", "Fishingasalivelihoodactivity", 
"Governance", "Roleofshadowstateactors"), class = "factor"), 
    Village = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L), .Label = c("Andra", "lahapau", "Pelipowai", "Ponam", 
    "Tulu"), class = "factor"), Variables = structure(c(13L, 
    3L, 10L, 11L, 12L, 16L, 5L, 8L, 1L, 2L, 15L, 17L, 6L, 14L, 
    9L, 4L, 7L, 13L, 3L, 10L, 11L, 12L, 16L, 5L, 8L, 1L, 2L, 
    15L, 17L, 6L, 14L, 9L, 4L, 7L, 13L, 3L, 10L, 11L, 12L, 16L, 
    5L, 8L, 1L, 2L, 15L, 17L, 6L, 14L, 9L, 4L, 7L, 13L, 3L, 10L, 
    11L, 12L, 16L, 5L, 8L, 1L, 2L, 15L, 17L, 6L, 14L, 9L, 4L, 
    7L, 13L, 3L, 10L, 11L, 12L, 16L, 5L, 8L, 1L, 2L, 15L, 17L, 
    6L, 14L, 9L, 4L, 7L), .Label = c("alternativelivelihood", 
    "anyactorsinvolvedinsustainability", "Attributionfactors", 
    "discusswithelectedleaders", "Effortsdirectedtoreducerisks", 
    "fishercommunityinfluence", "Infrastructureeffectiveness", 
    "multiplicityofactors", "Occupationforchildren", "Reversibility", 
    "Riskasamajorconsideration", "Riskbeingaddressed", "Statusoffisheries", 
    "Timefishing", "Whatwasdone", "Whoisatrisk", "whowasinvolved?" 
    ), class = "factor"), legend.var = structure(c(1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
    17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
    14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
    11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
    16L, 17L), .Label = c("a", "b", "c", "d", "e", "f", "g", 
    "h", "i", "j", "k", "l", "m", "n", "o", "p", "q"), class = "factor"), 
    score = c(1, 0.97, 1, 0.76, 0.794, 1, 0.71, 0.9, 0.5, 1, 
    1, 1, 1, 0.49, 0.72, 1, 0.7, 1, 1, 0, 0.67, 0.5, 1, 0.2, 
    1, 1, 0.7, 0.4, 0.5, 0.3, 0.67, 0.5, 0.7, 0.8, 1, 0, 0.46, 
    0.56, 0.375, 1, 0.13, 0.3, 0.5, 0.3, 0.3, 0.4, 0.6, 1, 1, 
    0.7, 0.8, 1, 0.86, 0.69, 0.51, 0.429, 1, 0.44, 0.3, 0.5, 
    0.6, 0.6, 0.7, 0.8, 0.4, 0.79, 0.8, 1, 1, 0.82, 0.85, 0.25, 
    0.226, 1, 0.18, 0.1, 1, 0.7, 0.3, 0.6, 0.3, 0.48, 0.16, 0.4, 
    0.8)), .Names = c("Indicator", "Village", "Variables", "legend.var", 
"score"), class = "data.frame", row.names = c(NA, -85L)) 

I Polardiagramme gemacht haben, wie folgt:

library(ggplot2) 
ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') + 
    geom_bar(width = 1, alpha=0.5, stat="identity") + 
    scale_y_continuous() + 
    coord_polar() + 
    theme(axis.ticks = element_blank()) + 
    facet_wrap(~Village, nrow=2, ncol=3) + 
    guides(colour = guide_legend(title.hjust = 0.5)) 

enter image description here

Wie Sie sehen können, Die Daten Spalte legend.var sind in der Spalte indicator gruppiert. Ich möchte eine Tabelle oder eine zweite Legende einfügen, die jeden der Indikatoren mit der Spalte und variables verknüpft. Im Idealfall, wenn dies als zweite Legende eingefügt wird, hätten die eindeutigen Elemente, die in der Spalte legend.var enthalten sind, dieselbe Füllfarbe wie der entsprechende Indikator. Die Fülllegende basiert auf der Spalte indicator. Die eingefügte Tabelle/Zusatzlegende würde die Spalten legend.var mit einem eindeutigen Alphabet und die Spalte variables mit der Bedeutung des jeweiligen Alphabets umfassen. Diese können dann die gleiche Füllfarbe haben wie die entsprechende indicator. Ich hoffe, das ist klar.

+2

Ich habe Ihren Code so geändert, dass er ohne Warnungen mit der aktuellen Version von ggplot2 ausgeführt wird. Allerdings verstehe ich dein Ziel nicht. Können Sie ein Modell bereitstellen? – Roland

+1

@Roland Ich denke, was OP will, ist eine Nachschlagetabelle für die Buchstaben bereitzustellen. Er zeichnet Buchstaben auf der X-Achse des Balkendiagramms, weil die Namen zu lang sind. Jetzt möchte er eine Tabelle, die den Buchstaben und den vollständigen Namen enthält, wie in 'Variablen' angegeben. – nograpes

Antwort

4

Hier ist eine Lösung mit einem gtable:

library(ggplot2) 
p <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') + 
    geom_bar(width = 1, alpha=0.5, stat="identity") + 
    scale_y_continuous() + 
    coord_polar() + 
    theme(axis.ticks = element_blank()) + 
    facet_wrap(~Village, nrow=2, ncol=3) + 
    guides(colour = guide_legend(title.hjust = 0.5)) + 
    theme(legend.position=c(0.85,0.25)) 

#create table 
library(gridExtra) 
tab <- tableGrob(unique(data[, c("legend.var", "Variables")]), 
       show.rownames=FALSE, gpar.coretext=gpar(fontsize=10), 
       gpar.coltext=gpar(fontsize=10, fontface='bold'), 
       gpar.corefill = gpar(fill = "grey90", col = "white"), 
       gpar.colfill = gpar(fill = "grey80", col = "white")) 

#arrange grobs 
library(gtable) 
a <- gtable(unit(c(0.7, 0.3) ,c("npc")), unit(1, "npc")) 
a <- gtable_add_grob(a, ggplotGrob(p),1,1) 
a <- gtable_add_grob(a, tab,1,2) 

#plot 
grid.draw(a) 

enter image description here

+1

zu erforschen Nette Lösung! +1 – Henrik

4

Für den Anfang können Sie etwas wie dies versuchen. Sie müssen Anordnung und Layout nach Ihren eigenen Vorlieben anpassen.

library(ggplot2) 
library(gridExtra) 

gg <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') + 
    geom_bar(width = 1, alpha=0.5, stat="identity") + 
    coord_polar() + 
    theme(axis.ticks = element_blank()) + 
    facet_wrap(~Village, nrow=2, ncol = 3) 

# create a table that translates legend.var to Variables 
tbl <- unique(data[ , c("legend.var", "Variables")]) 

# create a table grob 
tt <- tableGrob(d = tbl, 
       col.just = "left", 
       gpar.coretext = gpar(col = "black", cex = 0.5), 
       gpar.coltext = gpar(col = "black", cex = 0.5, fontface = "bold"), 
       gpar.rowtext = gpar(col = "black", cex = 0.5, fontface = "italic")) 

# arrange plot and table grob 
grid.arrange(gg, tt, ncol = 2) 

Update mit einem quick and dirty ggplot-only alternative

# create labels 
labs <- with(tbl, paste(legend.var, Variables)) 

gg <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator, col = Variables)) + 
    geom_bar(width = 1, alpha = 0.5, stat = "identity") + 
    coord_polar() + 
    theme(axis.ticks = element_blank()) + 
    facet_wrap(~ Village, nrow = 2, ncol = 3) + 
    scale_color_grey(labels = labs, start = 0.8, end = 0.9) 

gg 

enter image description here

+1

Das funktioniert ... aber gibt es irgendeinen "kanonischen" 'ggplot' Weg dies zu tun? Gibt es eine Möglichkeit, 'scale_x_discrete' mit einem" guide "zu versehen, mit dem Sie alternative Bezeichnungen vergeben können? – nograpes

+0

@nograpes, guter Punkt! Ich wette, es gibt einen ggplot-only-Weg. Ich dachte entlang dieser Route, aber steckte fest ... – Henrik

+0

@nograpes Ich glaube nicht. Und ich bin mir nicht sicher, ob ich dem Konzept eines Leitfadens für 'scale_x_discrete' folgen kann. – Roland

Verwandte Themen