我有如下所示的資料集:
CrimeRate Southern
56.6 1
60.3 1
79.8 1
85.6 0
96.8 0
97.4 0
98.7 1
99.9 1
103 1
106.6 1
109.4 1
112.1 0
114.3 0
121.6 1
123.4 1
145.4 1
其中 1 表示南方,0 表示非南方。
我想用圖表來比較南部和非南部各州的犯罪率。
到目前為止,這是我的代碼,但出現錯誤,如何繪制圖表?
compare_crime <- aggregate(crimeData$crime_rate ~ crimeData$southern, data=crimeData, FUN=sum)
hist(compare_crime)
uj5u.com熱心網友回復:
這是一種選擇:
library(tidyverse)
ggplot(df, aes(x=factor(Southern), y=CrimeRate))
geom_bar(stat="identity") theme_minimal()
輸出

或者如果你想要一個直方圖,那么你可以這樣做:
library(hrbrthemes)
ggplot(df, aes(x=CrimeRate, fill=factor(Southern)))
geom_histogram( color="#e9ecef", alpha=0.6, position = 'identity')
scale_fill_manual(values=c("#69b3a2", "#404080"))
theme_ipsum()
labs(fill="")
輸出

資料
crimeData <- structure(list(CrimeRate = c(56.6, 60.3, 79.8, 85.6, 96.8, 97.4,
98.7, 99.9, 103, 106.6, 109.4, 112.1, 114.3, 121.6, 123.4, 145.4
), Southern = c(1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L,
0L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, -16L
))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/451542.html
下一篇:在ggplot2中編輯分組箱線圖
