2016-05-02 7 views
1

Ich versuche, ein Balkendiagramm mit diesem Datensatz unterBalkendiagramm - Lutscher Charts - vizualization

df = structure(list(Affiliation = structure(c(1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L), .Label = c("BMI", "CCS", 
"CS", "Epi", "Genom", "HSE", 
"HSR", "HPR"), class = "factor"), 
    count = structure(c(4L, 21L, 14L, 20L, 11L, 13L, 19L, 15L, 
    5L, 22L, 17L, 24L, 9L, 12L, 18L, 16L, 1L, 10L, 7L, 23L, 2L, 
    3L, 8L, 6L), .Label = c("15", "26", "27", "32", "40", "41", 
    "42", "58", "62", "63", "70", "88", "89", "96", "99", "112", 
    "125", "160", "164", "172", "176", "178", "200", "628"), class = "factor"), 
    Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2014", 
    "2015", "2016"), class = "factor")), .Names = c("Affiliation", 
"count", "Year"), row.names = c(NA, 24L), class = "data.frame") 

zu erstellen Dies ist, was ich habe

bisher getan
ggplot(df, aes(x = Affiliation, y = count, fill = Year, group = Year)) + 
    geom_bar(position = position_dodge(width = 0.9), stat = "identity", alpha = 1, 
    size = 1, width = 0.05) + 
    geom_text(aes(label = count), position = position_dodge(width = 0.9), 
    vjust = -0.25) + scale_fill_brewer(palette = "Set1") 

aber ich zu erstellen versuchen eine Visualisierung für dieses Balkendiagramm auf diesem Beispiel basieren:

example

, die in diesem Post diskutiert wurde: Looking for better way to visualise distribution in R and ggplot2

+1

Wo ist 'bgcolor' definiert? Und was genau ist die Frage hier? – MrFlick

+0

Was ist 'bgcolor'? Zerhacke es und es läuft. Wenn Sie einen 'data' Parameter in' ggplot' setzen, müssen Sie nicht '$' -notation; Sie können einfach den Namen der nicht angegebenen Variablen eingeben. – alistaire

+0

@alistaire, danke alistaire Ich habe 'bgcolor' benutzt, um den Hintergrund transparent zu machen. Ich habe das jetzt entfernt. Ich stimme Ihnen bei der Verwendung von $ zu –

Antwort

5

Ich bin immer noch nicht ganz sicher, was Ihre Frage ist. Das Beispiel, das die Lollipop-Diagramme erstellt, verwendet geom_bar() nicht. Ist es das wonach du suchst?

# Why did you have this as a factor? 
df$count <- as.numeric(as.character(df$count)) 

gg <- ggplot(df, aes(Affiliation, count)) 
gg <- gg + geom_segment(aes(xend=Affiliation, yend=0)) 
gg <- gg + geom_point() 
gg <- gg + geom_text(aes(label=count, y=count+25), vjust=0, size=3) 
gg <- gg + scale_y_continuous(expand=c(0,0), limits=c(0, 800)) 
gg <- gg + facet_wrap(~Year) 
gg <- gg + labs(x=NULL, y=NULL) 
gg <- gg + theme_bw() 
gg <- gg + theme(strip.background=element_blank()) 
gg <- gg + theme(strip.text=element_text(hjust=0)) 
gg <- gg + theme(panel.grid.major.x=element_blank()) 
gg <- gg + theme(panel.grid.minor.y=element_blank()) 
gg <- gg + theme(axis.ticks=element_blank()) 
gg <- gg + theme(axis.text.x=element_text(size=8)) 
gg <- gg + theme(axis.text.y=element_text(size=8, vjust=c(0, 0.5, 0.5, 0.5, 1))) 
gg 

enter image description here

Auch nach dem Kommentar Antwort klar es ist immer noch nicht das, was Sie nach.

gg <- ggplot(df) 
gg <- gg + geom_segment(aes(x=Year, y=count, xend=Year, yend=0, color=Year), show.legend=FALSE) 
gg <- gg + geom_point(aes(x=Year, y=count, color=Year)) 
gg <- gg + geom_text(aes(label=count, x=Year, y=count+40), vjust=0, size=3) 
gg <- gg + scale_y_continuous(expand=c(0,0), limits=c(0, 800)) 
gg <- gg + ggthemes::scale_color_tableau(name="") 
gg <- gg + facet_wrap(~Affiliation, nrow=1, scales="free_x") 
gg <- gg + labs(x=NULL, y=NULL) 
gg <- gg + theme_bw() 
gg <- gg + theme(strip.background=element_blank()) 
gg <- gg + theme(strip.text=element_text(hjust=0)) 
gg <- gg + theme(panel.grid.major.x=element_blank()) 
gg <- gg + theme(panel.grid.minor.y=element_blank()) 
gg <- gg + theme(axis.ticks=element_blank()) 
gg <- gg + theme(axis.text.x=element_text(size=8)) 
gg <- gg + theme(axis.text.y=element_text(size=8, vjust=c(0, 0.5, 0.5, 0.5, 1))) 
gg <- gg + theme(legend.key=element_blank()) 
gg <- gg + theme(legend.position="bottom") 
gg 

enter image description here

Verwandte Themen