geom_bar

Published by onesixx on

dataDT <- as.data.table(cars, keep.rownames=T)
dataDT[ , `:=`(rn=factor(rn), speed=factor(speed))]
print(dataDT)
    rn speed dist
 1:  1     4    2
 2:  2     4   10
 3:  3     7    4
 4:  4     7   22
 5:  5     8   16
 6:  6     9   10
 ...
45: 45    23   54
46: 46    24   70
47: 47    24   92
48: 48    24   93
49: 49    24  120
50: 50    25   85
    rn speed dist

geom_bar(stat="count")

dataDT %>% ggplot(aes(x=speed)) + geom_bar()
dataDT %>% ggplot(aes(x=speed)) + geom_bar(stat="count")  # same
dataDT %>% ggplot(aes(x=speed)) + stat_count()            # same

dataDT[ , .(spped_cnt=.N), by=speed]
dataDT %>% ggplot(aes(x=speed)) + geom_bar(stat="identity") # ERROR:  y값 필요
dataDT %>% ggplot(aes(x=speed)) + geom_col()                # ERROR:  y값 필요

geom_bar(stat="identity")

dataDT %>% ggplot(aes(x=speed, y=dist, fill=dist), color="black") + 
           geom_col()
dataDT %>% ggplot(aes(x=speed, y=dist, fill=dist), color="black") + 
           geom_bar(stat="identity")

dataDT[ , .(dist_sum=sum(dist)), by=speed]
dataDT %>% ggplot(aes(x=speed, y=dist)) + geom_bar()  #Error: x 또는 y 하나만

Categories: ggplot2

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x