1
我用ggplot創建了盒子圖,但是我想根據我使用匯總統計創建的不同數據框中列的順序來更改y軸的順序。基於數據幀列的Y軸ggplot盒圖
這是腳本。腳本下面是對我所需輸出的描述。
#data
df <- data.frame(City = c("NY", "AMS", "BER", "PAR", "NY", "AMS", "AMS", "PAE"),
Time_Diff = c(4, 2, 7, 9, 2, 1, 10, 9),
Outliers = c(0, 0, 0, 0, 0, 1, 1, 0))
#data summary
summary <- df %>%
group_by(City) %>%
summarise(Median = median(Time_Diff),
IQR = IQR(Time_Diff),
Outliers = sum(Outliers)) %>%
arrange(desc(Median), desc(IQR), desc(Outliers))
summary <- as.data.frame(summary)
# Create ggplot object
bp <-ggplot(data = df, aes(x = reorder(City, Time_Diff, FUN = median), y= Time_Diff)) # Creates boxplots
# Create boxplot figure
bp +
geom_boxplot(outlier.shape = NA) + #exclude outliers to increase visibility of graph
coord_flip(ylim = c(0, 25)) +
geom_hline(yintercept = 4) +
ggtitle("Time Difference") +
ylab("Time Difference") +
xlab("City") +
theme_light() +
theme(panel.grid.minor = element_blank(),
panel.border = element_blank(), #remove all border lines
axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"), #add x-axis border line
axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black")) #add y-axis border line
我想是y軸(翻轉的x軸)的順序是相同的,如發明內容數據幀City列的順序。這意味着:
從上到下:PAE,PAR,BER,NY,AMS
任何有效的和優雅的建議?
SOLUTION
謝謝Prradep,我用您的解決方案的腳本和它的作品。我稍微調整了它,這樣我就不必再次鍵入軸的值。我重新使用了數據框中的城市矢量。這是我使用的腳本:
#data
df <- data.frame(City = c("NY", "AMS", "BER", "PAR", "NY", "AMS", "AMS", "PAE"),
Time_Diff = c(4, 2, 7, 9, 2, 1, 10, 9),
Outliers = c(0, 0, 0, 0, 0, 1, 1, 0))
#data summary
summary <- df %>%
group_by(City) %>%
summarise(Median = median(Time_Diff),
IQR = IQR(Time_Diff),
Outliers = sum(Outliers)) %>%
arrange(desc(Median), desc(IQR), desc(Outliers))
summary <- as.data.frame(summary)
# Preproces data for figure
order_city <- summary$City
# Create ggplot object
bp <-ggplot(data = df, aes(x = reorder(City, Time_Diff, FUN = median), y= Time_Diff)) # Creates boxplots
# Create boxplot figure
bp +
geom_boxplot(outlier.shape = NA) + #exclude outliers to increase visibility of graph
coord_flip(ylim = c(0, 25)) +
geom_hline(yintercept = 4) +
ggtitle("Time Difference") +
ylab("Time Difference") +
xlab("City") +
theme_light() +
theme(panel.grid.minor = element_blank(),
panel.border = element_blank(), #remove all border lines
axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"), #add x-axis border line
axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black")) + #add y-axis
scale_x_discrete(limits = rev(order_city)) #this is the function to change the order of the axis
謝謝!我對最終解決方案的代碼進行了一些修改。看到我的代碼問題 – SHW