简体   繁体   中英

How to plot advance hist in R

I'm traslating from Excel to R in order to achieve better result. So actually i got a data.frame like this:

A B C D E F G
0 0 0 0 0 0 0
2 0 0 0 0 0 0
2 0 0 2 0 0 1
1 0 0 2 0 1 0

So [A:G] are the name of the columns that can just contain 0, 1 or 2 as number. What i would like to do is plot a histogram or whatever in order to have one bar that rapresent one column, that should be divided as percentage (betweeen 0, 1 and 2), with all the column in the same graph.

在此处输入图片说明

From the image we can also see that on y-axis i prefer to see 0 to 100 and not the number of the rows, but again from a percentage perspective. The previous image is exactly what i need (also with the possibility to customize colors etc..) but for 7 columns.

Thanks a lot, Andrea.

Not sure if this is the slickest way to do it, but I think it does what you are looking for.

v <- c(0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 1, 1, 0, 0, 2, 0, 1, 0)
mtx <- data.frame(matrix(v, 4, 7, byrow = T))
names(mtx) <- c('A', 'B', 'C', 'D', 'E', 'F', 'G')

barplot(as.matrix(mtx))

new.df <- data.frame()
for(i in c('A', 'B', 'C', 'D', 'E', 'F', 'G')){
       tmp.v <- mtx[,i]
       for(j in tmp.v){
              tmp.df <- data.frame(C = i,
                                Val = j)
              new.df <- rbind(new.df, tmp.df)
       }


}

new.df$Val <- as.factor(new.df$Val)

df <- new.df %>% group_by(C, Val) %>% summarize(n = n()) %>% mutate(freq = n/sum(n))

df$freq <- as.factor(df$freq)
ggplot(df, aes(C, freq, fill= Val)) + geom_bar(stat = "identity", position = "stack")

Bar Plot

I'm not sure what you're asking. If this is about how to reproduce the plot you're showing, here is a how:

library(tidyverse);
mtcars %>%
    count(cyl, am) %>%
    mutate(am = factor(am, levels = c("1", "0")), cyl = as.factor(cyl)) %>%
    ggplot(aes(x = cyl, y = n, fill = am)) +
        geom_bar(stat = "identity", position = "fill") + 
        labs(y = "pct");

在此处输入图片说明

We can generate a similar plot based on the sample data you provide:

# Your sample data
df <- read.table(text =
    "A B C D E F G
0 0 0 0 0 0 0
2 0 0 0 0 0 0
2 0 0 2 0 0 1
1 0 0 2 0 1 0", header = T)

library(tidyverse);
df %>%
    gather(key, value, 1:7) %>%
    count(key, value) %>%
    mutate(key = as.factor(key), value = as.factor(value)) %>%
    ggplot(aes(x = key, y = n, fill = value)) +
        geom_bar(stat = "identity", position = "fill") + 
        labs(y = "pct");

在此处输入图片说明

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