[R] barplot that displays sums of values of 2 y colums grouped by different variables
Jim Lemon
drjimlemon at gmail.com
Mon Jan 15 22:22:52 CET 2018
Hi Kenneth,
I don't know about ggplot, but perhaps this will help:
kddf<-read.table(text="city n y
mon 100 200
tor 209 300
edm 98 87
mon 20 76
tor 50 96
edm 62 27",
header=TRUE,stringsAsFactors=FALSE)
library(plotrix)
barpos<-barp(t(kddf[,2:3]),names.arg=kddf[,1],xlab="City",ylab="Sum",
main="Sums of values of 2 y columns",col=2:3)
legend(5,300,c("Sum of n","Sum, of y"),fill=2:3)
barlabels(barpos$x,barpos$y)
Jim
On Tue, Jan 16, 2018 at 3:59 AM, kenneth dyson
<kenneth at kidscodejeunesse.org> wrote:
> I am trying to create a barplot displaying the sums of 2 columns of data
> grouped by a variable. the data is set up like this:
>
> "city" "n" "y" <br>
> mon 100 200 <br>
> tor 209 300 <br>
> edm 98 87 <br>
> mon 20 76 <br>
> tor 50 96 <br>
> edm 62 27 <br>
>
> the resulting plot should have city as the x-axis, 2 bars per city, 1
> representing the sum of "n" in that city, the other the sum of "y" in that
> city.
>
> If possible also show the sum in each bar as a label?
>
> I aggregated the data into sums like this:
>
> sum_data <- aggregate(. ~ City,data=raw_data,sum)
>
> this gave me the sums per city as I wanted but for some reason 1 of the
> cities is missing in the output.
>
> Using this code for the plot:
>
> ggplot(sum_data,aes(x = City,y = n)) + geom_bar(aes(fill = y),stat =
> "identity",position = "dodge")
>
> gave be a bar plot with one bar per city showing the sum of y as a color
> gradient. not what I expected given the "dodge" command in geom_bar.
>
> Thanks.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list