簡體   English   中英

將 x 軸標簽與 R 中的條對齊

[英]Align the x-axis labels with the bars in R

我有一個這樣的數據集:

發泄 名詞
發泄 679
Vente en l'état futur d'achèvement 137

我使用了以下代碼:

g1 <- ggplot(data=df, aes(x = libnatmut, y = counts, fill=libnatmut)) +
  geom_bar(stat = "identity", width = 0.3, position=position_dodge2(preserve='single'))+
  labs(title = "Nature des mutations", x="Type de la vente",y="Nombre de ventes") +
  geom_text(aes(label = counts), vjust = -0.3) +
  theme(axis.text.x = element_text(angle = 17, hjust = 1)) + 
  theme(axis.text.x = element_text(face="bold", color="#993333",size=14, angle=0),
        axis.text.y = element_text(face="bold", color="#993333",size=14, angle=360),
        axis.title=element_text(size=22))+
  theme(plot.title = element_text(size=24))+
  theme(axis.line = element_line(colour = "#993333", 
                                 size = 1, linetype = "solid"))+
  scale_fill_manual(values= rep_len(wes_palette("Zissou1"), 10))+
  theme(legend.position="none")

ggsave(g1, filename = "libnatmut.png", dpi = 300, height = 30, width = 30, units = "cm")

正如我們在圖表上看到的,x 軸標簽與我的條形圖不對齊。 我嘗試了很多不同的東西,但我無法獲得居中的圖表。 在此處輸入圖像描述

這可能很容易,但我很久以前只使用過 R,所以我根本不是 ggplot2 的專家。

我稍微修改了 ggplot 代碼並使用了提供的數據。

  • 主要行動是刪除: theme(axis.text.x = element_text(angle = 17, hjust = 1)) +
library(wesanderson)
library(tidyverse)

ggplot(data=df, aes(x = Vente, y = Nombre, fill=Vente)) +
  geom_bar(stat = "identity", width = 0.3, position=position_dodge2(preserve='single'))+
  labs(title = "Nature des mutations", x="Type de la vente",y="Nombre de ventes") +
  geom_text(aes(label = Nombre), vjust = -0.3) +
  theme(axis.text.x = element_text(face="bold", color="#993333",size=14, angle=0),
        axis.text.y = element_text(face="bold", color="#993333",size=14, angle=360),
        axis.title=element_text(size=22))+
  theme(plot.title = element_text(size=24))+
  theme(axis.line = element_line(colour = "#993333", 
                                 size = 1, linetype = "solid"))+
  scale_fill_manual(values= rep_len(wes_palette("Zissou1"), 10))+
  theme(legend.position="none")

在此處輸入圖像描述

數據:

df <- structure(list(Vente = c("Vente", "Vente en l'état futur d'achèvement"
), Nombre = c(679L, 137L)), class = "data.frame", row.names = c(NA, 
-2L))

暫無
暫無

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

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