如何重新缩放y轴以查看比例量?

时间:2019-03-28 18:32:31

标签: r ggplot2

我有大约70k obs的数据集。我想将它们绘制在具有5个(或更多)不同因子的x轴上,并通过三种不同严重程度的类型进行包装。

主要问题是大多数obs聚集在1个因子(严重性= 3)中,所以我什至无法阅读其他2个。 ylim 对我没有帮助,因为它实际上< strong>完全更改结果,而不是将其设置为一定百分比。

我应该自己分开吗?还是有什么命令可以帮我做到这一点?

我将在图像下方附加以使问题更全面。

https://i.imgur.com/zYaHom6.png

我想根据严重程度来判断每个因素。

这是代码示例。

acc.10 <- read.csv("Accidents2010.csv")

install.packages("ggplot2")
library(ggplot2)
install.packages("stringr")
library(stringr)

acc.10$Road_Type <- as.factor(acc.10$Road_Type)
acc.10$X1st_Road_Class <- as.factor(acc.10$X1st_Road_Class)

ggplot(acc.10, aes(x = Road_Type )) +
  geom_bar(width = 0.4) +
  ggtitle("Accidents based on Road Type") +
  xlab("Road Type")

ggplot(acc.10, aes(x = acc.10$X1st_Road_Class )) +
  geom_bar(width = 0.4) +
  ggtitle("Accidents based on 1st Road Class") +
  xlab("1st Road Class")

data.10 <- acc.10[which(acc.10$X1st_Road_Class == 3),]

#we will check light conditions in order to 
data.10$Light_Conditions <- as.factor(data.10$Light_Conditions)

#we plot to see the distribution
ggplot(data.10, aes(x = Light_Conditions)) +
  geom_bar(width = 0.5) +
  ggtitle("Accidents based on Light Conditions") +
  xlab("Light Conditions")

ggplot(data.10[which(as.numeric(data.10$Accident_Severity) == 3),]
, aes(x = Light_Conditions)) +
  geom_bar(width = 0.5) +
  ggtitle("Accidents based on Light Conditions") +
  xlab("Light Conditions")

#We drill harder to see if there are connections of survivability

data.10$Accident_Severity <- as.factor(data.10$Accident_Severity)

ggplot(data.10, aes(x = Light_Conditions, fill = Accident_Severity)) +
  geom_bar(width = 0.5) +
  ggtitle("Accidents based on Light Conditions and Survivability") +
  xlab("Light Conditions")

# We will try to wrap them based on severity instead of the bar graph

ggplot(data.10, aes (x = Light_Conditions)) +
  geom_bar(width = 0.5) +
  ggtitle("Accident seperated by severity affected of Light Conditions") +
  facet_wrap(~Accident_Severity) +
  xlab("Light Conditions") +
  ylab("Total Count")

包含数据的文件在这里:https://data.gov.uk/dataset/cb7ae6f0-4be6-4935-9277-47e5ce24a11f/road-safety-data/datafile/4c03ef8d-992d-44df-8543-412d23f3661b/preview

1 个答案:

答案 0 :(得分:0)

非常感谢@Peter K他的解决方案有效

它不是y轴的百分比,但这并不重要,因为现在有数据

清晰可读。

我设置了示例代码

ggplot(data.10, aes (x = Light_Conditions)) +
  geom_bar(width = 0.5) +
  ggtitle("Accident seperated by severity affected of Light Conditions") +
  facet_wrap(~Accident_Severity, scales = 'free_y') +
  xlab("Light Conditions") +
  ylab("Total Count")

facet_wrap(〜Accident_Severity, scales ='free_y')命令解决了该问题

https://i.imgur.com/gyXV1EZ.png

这张照片在上面,但我没有发布它的名声。再次非常感谢。