简体   繁体   English

ggplot2 scale_x_discrete导致我的x轴消失?

[英]ggplot2 scale_x_discrete causes my x axis to dissapear?

I have a dataframe: 我有一个数据框:

dput(df)

structure(list(row = c("(BSO,0)", "(BSO,1)", "(BSO,2)", "(BSO,3)", 
"(BSO,4)", "(BSO,5)", "(BSO,6)", "(CAR,0)", "(CAR,1)", "(CAR,2)", 
"(CAR,3)", "(CAR,4)", "(GMA,0)", "(GMA,1)", "(GMA,2)", "(GMA,3)", 
"(GMA,4)", "(GRA,0)", "(GRA,1)", "(GRA,2)", "(GRA,3)", "(GRS,7)", 
"(MAI,0)", "(MAI,1)", "(MAI,3)", "(MAI,5)", "(MAI,6)", "(MAI,7)", 
"(MAI,8)", "(ONI,0)", "(ONI,1)", "(ONI,4)", "(ONI,41)", "(ONI,45)", 
"(ONI,48)", "(ONI,49)", "(POT,0)", "(POT,1)", "(POT,2)", "(POT,3)", 
"(POT,5)", "(POT,6)", "(POT,8)", "(POT,9)", "(SBA,3)", "(SBA,5)", 
"(SBA,7)", "(SBA,8)", "(SBA,9)", "(SBT,0)", "(SBT,1)", "(SBT,2)", 
"(SBT,11)", "(SBT,14)", "(SBT,19)", "(SBT,32)", "(SBT,39)", "(SCR,1)", 
"(SCR,2)", "(SCR,3)", "(SWH,5)", "(SWH,7)", "(SWH,8)", "(SWH,9)", 
"(TLP,0)", "(TLP,6)", "(TLP,12)", "(TLP,45)", "(TLP,48)", "(TLP,49)", 
"(VEG,1)", "(VEG,2)", "(VEG,8)", "(WBA,8)", "(WCR,0)", "(WCR,1)", 
"(WWH,2)", "(WWH,3)", "(WWH,5)", "(WWH,7)", "(WWH,8)", "(WWH,9)", 
"(WWH,29)"), numofpics = c(1484, 1299, 1923, 3494, 2259, 934, 
1962, 209, 2218, 63, 16, 2178, 69, 165, 491, 147, 217, 114, 988, 
4518, 969, 26, 45, 479, 1102, 103, 69, 557, 144, 514, 1398, 59, 
494, 217, 610, 113, 872, 779, 152, 364, 20, 774, 995, 631, 23, 
145, 34, 66, 107, 448, 307, 17, 68, 757, 51, 47, 3787, 318, 578, 
431, 214, 167, 20, 24, 261, 378, 154, 58, 20, 70, 925, 22, 42, 
21, 35, 142, 703, 750, 76, 643, 278, 273, 27), numofparcels = c(49, 
40, 63, 94, 68, 32, 61, 8, 32, 2, 1, 30, 2, 7, 19, 6, 10, 5, 
27, 52, 27, 1, 2, 20, 25, 4, 3, 23, 5, 19, 25, 2, 17, 7, 20, 
5, 29, 27, 4, 11, 1, 20, 25, 21, 1, 5, 1, 3, 4, 16, 10, 1, 2, 
27, 1, 1, 34, 12, 15, 16, 8, 6, 1, 1, 10, 9, 6, 2, 1, 3, 12, 
1, 1, 1, 1, 5, 16, 19, 3, 19, 8, 10, 1), crop = c("BSO", "BSO", 
"BSO", "BSO", "BSO", "BSO", "BSO", "CAR", "CAR", "CAR", "CAR", 
"CAR", "GMA", "GMA", "GMA", "GMA", "GMA", "GRA", "GRA", "GRA", 
"GRA", "GRS", "MAI", "MAI", "MAI", "MAI", "MAI", "MAI", "MAI", 
"ONI", "ONI", "ONI", "ONI", "ONI", "ONI", "ONI", "POT", "POT", 
"POT", "POT", "POT", "POT", "POT", "POT", "SBA", "SBA", "SBA", 
"SBA", "SBA", "SBT", "SBT", "SBT", "SBT", "SBT", "SBT", "SBT", 
"SBT", "SCR", "SCR", "SCR", "SWH", "SWH", "SWH", "SWH", "TLP", 
"TLP", "TLP", "TLP", "TLP", "TLP", "VEG", "VEG", "VEG", "WBA", 
"WCR", "WCR", "WWH", "WWH", "WWH", "WWH", "WWH", "WWH", "WWH"
), bbch = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 0L, 1L, 2L, 3L, 4L, 0L, 
1L, 2L, 3L, 4L, 0L, 1L, 2L, 3L, 7L, 0L, 1L, 3L, 5L, 6L, 7L, 8L, 
0L, 1L, 4L, 41L, 45L, 48L, 49L, 0L, 1L, 2L, 3L, 5L, 6L, 8L, 9L, 
3L, 5L, 7L, 8L, 9L, 0L, 1L, 2L, 11L, 14L, 19L, 32L, 39L, 1L, 
2L, 3L, 5L, 7L, 8L, 9L, 0L, 6L, 12L, 45L, 48L, 49L, 1L, 2L, 8L, 
8L, 0L, 1L, 2L, 3L, 5L, 7L, 8L, 9L, 29L)), .Names = c("row", 
"numofpics", "numofparcels", "crop", "bbch"), row.names = c(NA, 
83L), class = "data.frame")

I wish to graph with ggplot like so: 我希望像这样用ggplot绘制图形:

ggplot(data=df, aes(x=row, y=numofpics, fill = crop)) +
  geom_bar(stat='identity') +
  scale_x_discrete(breaks = unique(df$crop), labels = unique(df$crop))

in order to produce a graph like this: 为了产生这样的图:

with the only exception that the legend labels get displayed on the X axis. 唯一的例外是图例标签显示在X轴上。 For the life of me, I don't get why they instead disappear. 对于我的一生,我不知道为什么它们会消失。

Although my data is not numeric/continuous, I've tried converting to factor: 尽管我的数据不是数字/连续数据,但我尝试转换为系数:

ggplot(data=df, aes(x=factor(row), y=numofpics, fill = factor(crop))) +
  geom_bar(stat='identity') +
  scale_x_discrete(breaks = unique(df$crop), labels = unique(df$crop))

which produced the same result. 产生了相同的结果。

And I've tried this solution from another thread: 我从另一个线程尝试了此解决方案:

ggplot(data=df, aes(x=factor(row), y=numofpics, fill = factor(crop))) +
  geom_bar(stat='identity') +
  scale_x_discrete(limits=unique(df$crop)) 

which did this: 这样做的:

Can someone enlighten me? 有人可以启发我吗?

Based on what you described, it looks like you might want something like one of these, but please let me know if this isn't what you had in mind. 根据您的描述,您可能想要其中一种,但是如果您不是这样,请告诉我。

library(tidyverse)

ggplot(data=df, aes(x=str_extract(row, "[0-9]+"), y=numofpics, fill = crop)) +
  geom_bar(stat='identity') +
  facet_grid(. ~ crop, scales="free_x", space="free_x") +
  guides(fill=FALSE) +
  theme_bw() +
  theme(strip.text.x=element_text(angle=-90),
        axis.text.x=element_text(size=rel(0.8))) 

在此处输入图片说明

ggplot(data=df, aes(x=factor(bbch), y=numofpics, fill = crop)) +
  geom_bar(stat='identity') +
  geom_text(aes(label=bbch), vjust=-0.25, size=3) +
  facet_grid(. ~ crop, scales="free_x", space="free_x", switch="x") +
  guides(fill=FALSE) +
  theme_bw() +
  theme(strip.text.x=element_text(angle=-90),
        strip.placement="outside", 
        axis.text.x=element_text(size=rel(0.8))) 

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM