Вопрос

My stacked plot is going all wonky in the later years. Could someone look at my code and tell me where I went wrong? I thought my code was identical to the example code (both below).

I am not very experienced with ggplot2 plot stacking so I might be making a simple error.

I wanted something that looked like: Stacked Plot

The code to produce that produces the above stacked plot is available as part of The R Graphics Cookbook and can be found at Revolutions R Blog Post.
From The R Graphics Cookbook library(ggplot2) library(gcookbook)

 ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) +
 geom_area(colour="black", size=.2, alpha=.4) +
 scale_fill_brewer(palette="Blues", breaks=rev(levels(uspopage$AgeGroup)))

I thought I had pretty much duplicated the syntax. But, it seems to me that I must have made a mistake somewhere.

 lawsize <- as.data.frame(read.csv("https://raw.githubusercontent.com/EconometricsBySimulation/wild-monkey/master/usc.csv"))

 ggplot(lawsize, aes(x=Year, y=KB, fill=factor(Name)))+
 geom_area(colour="black", size=.2, alpha=.4)
 scale_fill_brewer(palette="Blues", breaks=rev(levels(lawsize$Name)))

My Stacked Plot

Thanks for your consideration in this matter! F

Это было полезно?

Решение

You definitely need to follow Sandy Muspratt's suggestions, but even then you'll have over 60 factors producing something like:

lawsize$Name <- factor(tolower(lawsize$Name))
lawsize = lawsize[!duplicated(lawsize), ]

gg <- ggplot(lawsize, aes(x=Year, y=KB, fill=Name))
gg <- gg + geom_area(colour="black", size=.2, alpha=.4)
gg <- gg + theme(legend.position="none")
gg

enter image description here

which is pretty, but—IMO—hardly informative. Perhaps thinking more about what story you're trying to communicate before picking a visualization would be in order?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top