首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当class为整数时,每隔一个x轴标签在ggplot2中绘制,但在class为character时有效

当class为整数时,每隔一个x轴标签在ggplot2中绘制,但在class为character时有效
EN

Stack Overflow用户
提问于 2021-07-02 01:19:10
回答 1查看 25关注 0票数 0

首先,我在sjPlot中使用set.theme()来设置我的ggplot的主题:

代码语言:javascript
复制
# library(ggplot2)
# library(RColorBrewer)
# library(sjPlot)
# library(tidyr)

set_theme(
  geom.outline.color = "antiquewhite4", 
  geom.outline.size = .5, 
  geom.label.size = 1.5,
  geom.label.color = "blue50",
  title.color = "blue", 
  title.size = 1.5, 
  title.align = 'center',
  # axis.angle.x = 90, 
  axis.textcolor = "blue", 
  axis.textsize = 1.15,
  base = theme_bw(),
)

从这段代码中,我不清楚是否应该绘制所有其他的x轴标签。当列类是整数时,这是result,每隔一年标记一次。一种解决办法是将类更改为字符,这将绘制correctly。以下是数据帧:

代码语言:javascript
复制
df<-structure(list(Year = c(2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 
2020L, 2020L, 2020L), Type = c("Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought"), `Number of Counties` = c(0L, 
0L, 0L, 0L, 3L, 1L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 5L, 5L, 19L, 19L, 0L, 5L, 0L, 0L, 0L, 5L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 3L, 3L, 0L, 0L, 0L, 3L, 3L, 3L, 0L, 8L, 
0L, 3L, 0L, 0L, 10L, 8L, 3L, 3L, 0L, 0L, 0L, 9L, 4L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 4L, 0L, 4L, 19L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 19L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 24L)), row.names = c(NA, 
-108L), class = c("tbl_df", "tbl", "data.frame"))

和我的代码

代码语言:javascript
复制
# check class
class(df$Year) # [1] "integer"

## ggplot

# interpolate a color palette 
colourCount = length(unique(df$Type))
getPalette = colorRampPalette(brewer.pal(10, "RdBu"))

# make plot
ggplot(df)+
  geom_bar(aes(x=Year, y=`Number of Counties`, fill=Type), stat="identity") +
  scale_fill_manual(values = getPalette(colourCount)) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), legend.key.height=unit(.75, "cm"))+
  ggtitle('VT Disaster Designations: 2012-2020\nNote some counties have more than one\ndisaster type per year')


#change year class to character

df$Year<-as.character(df$Year)

ggplot(df)+
  geom_bar(aes(x=Year, y=`Number of Counties`, fill=Type), stat="identity") +
  scale_fill_manual(values = getPalette(colourCount)) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), legend.key.height=unit(.75, "cm"))+
  ggtitle('VT Disaster Designations: 2012-2020\nNote some counties have more than one\ndisaster type per year')

如果您对sjPlot和/或ggplot有任何帮助,我们将非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2021-07-02 02:32:06

我不知道为什么这个类使您的绘图跳过每隔一个x轴刻度/标签,但是您可以通过使用scale_x_continuous(breaks = ...)指定x轴断点来解决这个问题。

代码语言:javascript
复制
# your current code with df$Year as integer
p <- ggplot(df)+
  geom_bar(aes(x=Year, y=`Number of Counties`, fill=Type), stat="identity") +
  scale_fill_manual(values = getPalette(colourCount)) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), legend.key.height=unit(.75, "cm"))+
  ggtitle('VT Disaster Designations: 2012-2020\nNote some counties have more than one\ndisaster type per year')

# specify the x-axis breaks
p + scale_x_continuous(breaks = 2012:2020)

p + scale_x_continuous(breaks = df$Year) # alternative
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68214438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档