How to label percentages within a stacked hatch plot using an R-stem

I am new to R. I would like others to explain to me how to add absolute values ​​inside individual stacked bars in a consistent manner using the basic R plot function (base R). I tried to plot an overlaid histogram using the R base, but the values ​​are displayed in an inconsistent / illogical way such that it should be 100% for each village, but they don't add up to 100%. Here's the data I'm working on:

Village      100         200       300  400      500

Male    68.33333    53.33333        70   70 61.66667

Female  31.66667    46.66667        30   30 38.33333

      

Therefore, there are five villages and data showing heads of households interviewed for sex.

I used the following command to plot the graph:

barplot(mydata,col=c("yellow","green")
x<-barplot(mydata,col=c("yellow","green")
text(x,mydata,labels=mydata,pos=3,offset=.5)

      

Please help highlight the correct values ​​in each bar Thanks

+3


source to share


1 answer


It started out as a comment, but it seemed unfair not to turn into an answer. To answer your question correctly (even a stack overflow), we need to know how "mydata" is structured. At first I assumed it was a data frame with 5 rows and 2 or 3 columns, but in this case your code doesn't make sense. However, if it were the way it is structured, this is one way to do what I think you need:

mydata <- data.frame(
    row.names =c(100, 200, 300, 400, 500),
    Male =c(68.33333, 53.33333, 70, 70, 61.66667),
    Female =c(31.66667, 46.66667, 30, 30, 38.33333))

x <- barplot(t(as.matrix(mydata)), col=c("yellow", "green"), 
    legend=TRUE, border=NA, xlim=c(0,8), args.legend=
        list(bty="n", border=NA), 
    ylab="Cumulative percentage", xlab="Village number")
text(x, mydata$Male-10, labels=round(mydata$Male), col="black")
text(x, mydata$Male+10, labels=100-round(mydata$Male))

      

which produces the following:

enter image description here

An alternative would be to set the y value to 40 for all male text labels and 80 for all women - this would have the advantage of less confusing lettering jitter and the disadvantage that the vertical position of the text is no longer data-bound.



Personally, I don't like this barplot at all, although there are many much worse data visualization crimes than a simple barplot. The numbers in the parcels clutter up and reduce the visual impact of the actual data display on colors, shapes, and sizes. I would prefer a simple plot plot like:

library(ggplot2)
ggplot(mydata, aes(x=row.names(mydata), y=Male)) +
  geom_point(size=4) +
  coord_flip() +
  labs(x="Village number\n", y="Percentage male") +
  ylim(0,100) +
  geom_hline(yintercept=50, linetype=2)

      

which gives

enter image description here

The graph has less excess clutter, higher data-to-ink ratio, and more. At the end, however, you need to create a storyline that means something to your audience.

+13


source







All Articles