R语言 我正在寻找一个解决我的阴谋问题的方法

xyhw6mcr  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(78)

这是我的数据:dput(test_data):

structure(list(day = c("11. Februar", "12. Februar", "13. Februar", 
"14. Februar", "15. Februar", "16. Februar", "18. Februar", "19. Februar", 
"20. Februar", "21. Februar", "22. Februar", "23. Februar", "25. Februar", 
"26. Februar", "27. Februar", "28. Februar", "01. März", "02. März", 
"04. März", "05. März", "06. März", "07. März", "08. März", 
"09. März", "11. März"), mean_score = c(-0.0544601918465228, 
-0.0261774456521739, -0.0194014563106796, -0.0700457858769932, 
-0.0344666666666667, -0.0431649230769231, -0.0395243346007605, 
0.00460614406779661, -0.0536940711462451, -0.0243264444444444, 
-0.0295975683890577, -0.0554590509666081, -0.0600848484848485, 
-0.0214302375809935, -0.00898032490974729, -0.0516276397515528, 
-0.0820686330935252, -0.054661620294599, -0.049621686746988, 
-0.0169375654450262, -0.0215138719512195, -0.0119615160349854, 
-0.00512207207207207, -0.0409468584405753, -0.0158904621435595
)), row.names = c(NA, -25L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x0000022386fef540>)

现在我想用下面的代码绘制这个数据:

test_data %>%
  ggplot(aes(x = day, y = mean_score)) +
  stat_smooth(aes(group = 1), method = "loess", se = FALSE, size = 2.5, color = "black") +
  scale_y_continuous(limits = c(-0.08, 0.001), expand = c(0, 0)) +
  scale_x_discrete(breaks = test_data$day, 
                   labels = ifelse(test_data$day == "27. Februar", "27. Februar", "")) +
  xlab(" ") +
  ylab("Sentiment Score") +
  ggtitle("Raiffi Verlauf über 30 Tage") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        plot.title = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major.y = element_line(color = "gray80"))

情节如下所示:

现在我可以提出我的问题:为什么x轴标签“27. Februar”(德语为“27 Februar”)位于图的末尾?当您查看test_data时,您会看到“27. Februar”实际上位于数据表的中间。test_data从11. Februar(11 February)开始,到11. März(11 March)结束。因此,上面的图没有正确表示我的数据。我如何纠正这个问题?
谢谢你的帮助。
最好
马苏皮拉米

6rqinv9w

6rqinv9w1#

“27. Februar”出现在图的末尾,因为x轴按ggplot2排序。如果删除条件标签,您将看到轴以March开始:

library(ggplot2)
library(dplyr)

test_data %>%
  ggplot(aes(x = day, y = mean_score)) +
  stat_smooth(aes(group = 1), method = "loess", se = FALSE, size = 2.5, color = "black") +
  scale_y_continuous(limits = c(-0.08, 0.001), expand = c(0, 0)) +
  scale_x_discrete(breaks = test_data$day,) +
  xlab(" ") +
  ylab("Sentiment Score") +
  ggtitle("Raiffi Verlauf über 30 Tage") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        plot.title = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major.y = element_line(color = "gray80"))

你可以告诉ggplot2,这一天是一个有序的变量,factor(day, levels = day),现在2月27日出现在预期的地方。

library(ggplot2)
library(dplyr)

test_data %>%
  ggplot(aes(x = factor(day, levels = day), y = mean_score)) +
  stat_smooth(aes(group = 1), method = "loess", se = FALSE, size = 2.5, color = "black") +
  scale_y_continuous(limits = c(-0.08, 0.001), expand = c(0, 0)) +
  scale_x_discrete(breaks = test_data$day, 
                   labels = ifelse(test_data$day == "27. Februar", "27. Februar", "")) +
  xlab(" ") +
  ylab("Sentiment Score") +
  ggtitle("Raiffi Verlauf über 30 Tage") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        plot.title = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major.y = element_line(color = "gray80"))

创建于2023-04-26带有reprex v2.0.2

相关问题