簡體   English   中英

使用 dotplot (binaxis = "y") 繪圖時 x 軸錯誤 - ggplot

[英]x axis Error when plotting with dotplot (binaxis = "y") - ggplot

我正在使用 dominick 的數據集並分析 softdrink 文件。

我想顯示每家商店的總銷售額。

下面是我的代碼。

#########################################################################
################### Graph 1 - Total profit per Store.####################
#########################################################################
# Calculate the total profit per store. 
softdrinks %>%
  group_by(STORE) %>%
  summarise(totalProfit = sum(PROFIT)) -> storeProfit

# to highlight the highest and lowest profit
storeProfit %>%
  mutate(color = (min(totalProfit) == totalProfit | max(totalProfit) == totalProfit)) %>%
  filter(color == "TRUE")-> minMax



# display sales per each store.
ggplot(data = storeProfit, aes(x = factor(STORE), y = totalProfit)) +
  geom_dotplot(binaxis = "y", fill = "light blue") +
  geom_dotplot(data = minMax,
               binaxis = "y",
               aes(x = factor(STORE), y = totalProfit),
               fill = "red") +
  labs(title = "Total softdrink profit per Store", x = "Store", y = "Total Profit (USD$)") +
  theme(axis.text.x=element_text(angle=90,hjust=1))    # rotate the x axis labels by 90 degrees.
 

當我運行代碼時,我得到下圖。 在此處輸入圖片說明

你能幫我理解為什么我的 x 軸看起來很奇怪並幫助我解決這個問題嗎?

也許您正在考慮使用 geom_point 而不是 geom_dotplot ? 您的軸是離散的,因為您使用了一個因子變量,但實際上您在右側有一個空格。 也許您可以不使用兩個數據集,而是在一個數據集上執行 ggplot 並使用您創建的顏色變量為正確的點着色。

   storeProfit = storeProfit %>%mutate(color = (min(totalProfit) == totalProfit | max(totalProfit) == totalProfit))
  

ggplot(data = storeProfit, aes(x = factor(STORE), y = totalProfit,fill=color)) +
  geom_dotplot(binaxis = "y", fill = "light blue")+
  labs(title = "Total softdrink profit per Store", x = "Store", y = "Total Profit (USD$)") +
  theme(axis.text.x=element_text(angle=90,hjust=1))+
scale_fill_manual(c("light blue","red")) 

如果沒有可重復的數據集,這是我能想到的最好的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM