简体   繁体   English

将 x 轴标签与 R 中的条对齐

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

I have a data set like that :我有一个这样的数据集:

Vente发泄 Nombre名词
Vente发泄 679 679
Vente en l'état futur d'achèvement Vente en l'état futur d'achèvement 137 137

I used the following code :我使用了以下代码:

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")

As we can see on the chart, the x-axis labels aren't aligned with my bars.正如我们在图表上看到的,x 轴标签与我的条形图不对齐。 I tried a lot of different things, but I can't manage to obtain a centered chart.我尝试了很多不同的东西,但我无法获得居中的图表。 在此处输入图像描述

It's probably pretty easy, but I only used R a long time ago so I'm not an expert at all with ggplot2.这可能很容易,但我很久以前只使用过 R,所以我根本不是 ggplot2 的专家。

I modified the ggplot code a litte and used the data provided.我稍微修改了 ggplot 代码并使用了提供的数据。

  • Main action was to remove: theme(axis.text.x = element_text(angle = 17, hjust = 1)) +主要行动是删除: 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")

在此处输入图像描述

data:数据:

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