English 中文(简体)
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 20 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
原标题:Stacked barplot for a matrix with numbers as labels on the bars

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 17 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 15 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

3.89 4.97 8.56 7.17
35.67 42.73 51.68 50.09
48.30 43.48 34.95 38.24
12.14 8.82 4.81 4.50

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 14 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

nombres_comp <- c("Tac. 
 Urb",
                  "Ven. 
 Urb",
                  "Tac. 
 Game",
                  "Ven. 
 Game")

bp<-barplot(tabl,
        main = NULL,
        xlab = "(%)",
        ylab = NULL,
        axes = TRUE,
        horiz = TRUE,
        beside = FALSE,
        names.arg = nombres_comp,
        col = c("#487c82","#487c83", 
                "#81e0eb", "#b1ecf2")
        )
y <- tabl
text(bp+2,y,labels=as.character(y))

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 12 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 11 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 09 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

问题回答

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 08 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

library(ggplot2)
tabl <- structure(c(3.89, 35.67, 48.3, 12.14, 4.97, 42.73, 43.48, 8.82, 8.56, 51.68, 34.95, 4.81, 7.17, 50.09, 38.24, 4.5), dim = c(4L, 4L), dimnames = list(c("Tac. 
 Urb", "Ven. 
 Urb", "Tac. 
 Game", "Ven. 
 Game"), c("V1", "V2", "V3", "V4")))

rownames(tabl) <- nombres_comp
tabl_long <- as.data.frame.table(tabl)
tabl_long
#            Var1 Var2  Freq
# 1   Tac. 
 Urb   V1  3.89
# 2   Ven. 
 Urb   V1 35.67
# 3  Tac. 
 Game   V1 48.30
# 4  Ven. 
 Game   V1 12.14
# 5   Tac. 
 Urb   V2  4.97
# 6   Ven. 
 Urb   V2 42.73
# 7  Tac. 
 Game   V2 43.48
# 8  Ven. 
 Game   V2  8.82
# 9   Tac. 
 Urb   V3  8.56
# 10  Ven. 
 Urb   V3 51.68
# 11 Tac. 
 Game   V3 34.95
# 12 Ven. 
 Game   V3  4.81
# 13  Tac. 
 Urb   V4  7.17
# 14  Ven. 
 Urb   V4 50.09
# 15 Tac. 
 Game   V4 38.24
# 16 Ven. 
 Game   V4  4.50


ggplot(tabl_long, aes(x=Freq, y=Var1, fill=Var2)) +
  geom_col(position="fill", color = "black") +
  scale_x_continuous(labels = scales::percent_format()) +
  geom_text(aes(label = Freq), position = position_fill(vjust = 0.5)) +
  scale_fill_manual(values=c(V1="#487c82", V2="#487c83", V3="#81e0eb", V4="#b1ecf2")) +
  labs(x = "Nivel de Desempeño (%)", y = NULL)

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 06 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

  • If you want to change the order of the rows (e.g., Ven Game), then you need to use factor(Var, levels=c(...)) where the levels the exact values in the order you want.
  • I don t know what V1 through V4 (columns) are. I think it would be best to have them labeled as well, but over to you ... to change them, you can use a number of techniques for converting them, including a merge (base::merge, dplyr::left_join) of table with two columns, data.frame(Var2=c("V1","V2",..), RealName=c("Quux","Bar",..)), or dplyr::case_when, or frankly the easiest is likely colnames(tabl) <- c(...) before your convert to a long form.
tabl <- as.matrix(read.table(text = "3.89   4.97    8.56    7.17
                                     35.67  42.73   51.68   50.09
                                     48.30  43.48   34.95   38.24
                                     12.14  8.82    4.81    4.50", header = F))

nombres_comp <- c("Tac. 
 Urb", "Ven. 
 Urb",
                  "Tac. 
 Game", "Ven. 
 Game")

bp<-barplot(tabl,
            main = NULL, xlab = "(%)", ylab = NULL,
            axes = TRUE, horiz = TRUE, beside = FALSE,
            names.arg = nombres_comp,
            col = c("#487c82", "#487c83", "#81e0eb", "#b1ecf2"))

y <- apply(tabl/2 + rbind(rep(0, ncol(tabl)) , 
                          head(apply(tabl, 2, cumsum), -1)), 
           1, as.numeric)

l <- as.character(apply(tabl, 1, as.character))

text(bp,x = y , labels= l)

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 04 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 03 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签