简体   繁体   中英

ggplot2 or sjPlot sum stacked barplot columns

I am running R version 3.5.2 (2018-12-20) -- "Eggshell Igloo" on a MacBook Pro, OS 10.14.2.

I have tried a few methods to get these plots. My preferred method was trying to create a stacked barplot of my data (factor grouped over time, my x axis and the counts as the y), with a dichotomous variable count 0,1 counts in each column as they match the counts on the y axis. However I am flexible. I have this code that works if I can overlay a barplot on this that would help.

ggplot(dat, aes(x=factor(yr),y=n, group=(n>0)))+
  stat_summary(aes(color=(n>0)),fun.y=length, geom="line")+
  scale_color_discrete("Key",labels=c("NN", "N"))+
    labs(title=  "1992-2018", x="Years",y="n")

using my full dataset, I tried this and got really close to the stacked barplot, it gave me the correct counts per the "yr" variable, however for my variable "n" it gave me a continuous range 0-1.0.

p<-ggplot(data=dat, aes(x=dat$yr, y=n, fill=n)) +
+   geom_bar(stat="identity")

This is the data I am most interested in. I tried to then coerce it into a table then a data frame.

t2<- table(dat$yr, dat$n)

        0  1
  1992  6  0
  1993 10  0
  1994  3  1
  1995 20  2
  1996 15  2
  1997 16  0
  1998 16  0
  1999  9  3
  2000  5  0
  2001  5  1
  2002  7  1
  2003  9  2
  2004  4  3
  2005  6  3
  2006  5  3
  2007  6  3
  2008  4  3
  2009  8  4
  2010  7  1
  2011  4  5
  2012  4  5
  2013  6  2
  2014  0  2
  2015  3  3
  2016  5  5
  2017  4  4
  2018  8  5

t<-table(dat$yr)

1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 
 6   10    4   22   17   16   16   12    5    6    8   11    7    

2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017  
9     8    9    7    12    8    9    9    8    2    6   10    8    

2018
 13

I then tried:

df<- data.frame(t, t2)
head(df)
head(df)
  Var1  Freq         Var1.1.    Var2  Freq.1
1 1992    6          1992        0      6
2 1993   10          1993        0     10
3 1994    4          1994        0      3
4 1995   22          1995        0     20
5 1996   17          1996        0     15
6 1997   16          1997        0     16

p<-ggplot(data=df, aes(x=Var1, y=Var2)) +
   geom_bar(stat="identity")
  p      

replacing these for the dataset variables gave me worse results with the y-axis showing no counts per year for "yr" variable and each column was filled all the way to the top of the range of "1".

Again, I would like to get a stacked barplot with the binary "n" in each year column to show the 0/1 sum which should match the 'yr' counts on the y-axis. or, I can use the ggplot I got in the first code I posted and get the sums for each year there, I would take that as well.

this comes really really close. if it also gave a total at the top it would be perfect.

package sjPlot:

    sjp.grpfrq(dat$yr, dat$n,  bar.pos = c("stack"), show.values = TRUE, show.n = TRUE, show.prc = FALSE, title = NULL)

The major issue with the sjPlot code is I cannot change the legend labels. it shows n= 0, 1. I need to change this to be specific.

Thanks so much in advance!

Try this and see if that's what you want?

ggplot(data=df, aes(x=Var1, y=Freq)) +
   geom_bar(stat="identity")

Resolved.

sjp.grpfrq(dat$yr, dat$n, bar.pos = c("stack"), legend.title = "Key", legend.labels = c("NN", "N"), show.values = TRUE, show.n = TRUE, show.prc = FALSE, show.axis.values = TRUE, title = "1992-2018")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM