Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot图表中的混合格式轴标签_R_Ggplot2_Label_Axis - Fatal编程技术网

R ggplot图表中的混合格式轴标签

R ggplot图表中的混合格式轴标签,r,ggplot2,label,axis,R,Ggplot2,Label,Axis,是否可以使用带有日期格式标签和文本格式标签的ggplot轴 下面的代码生成一个带有日期格式的x轴的图表,该x轴从1月18日运行到6月19日 #create plot with line and points. #colour of points based on colour column of meltdf2 ggplot(data = meltdf2, aes(x = Month, y = variable, group = variable)) + geom_line(linetype

是否可以使用带有日期格式标签和文本格式标签的ggplot轴

下面的代码生成一个带有日期格式的x轴的图表,该x轴从1月18日运行到6月19日

#create plot with line and points.
#colour of points based on colour column of meltdf2
ggplot(data = meltdf2, aes(x = Month, y = variable, group = variable)) +
  geom_line(linetype = "dashed", colour = "grey") +
  geom_point(aes(colour = meltdf2$colour, size = 3)) +
  geom_point(data = melt_change, aes(shape = value, size = 3)) +
  scale_colour_manual(values = pal, limits = names(pal), guide_legend("Risk Level")) +
  scale_shape_manual(values = sh_pal, limits = names(sh_pal), guide_legend("Latest Change")) +
  scale_x_date(date_breaks = "1 month" , date_labels = "%b-%y") +
  theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(),
        axis.line.x = element_line(colour = "darkgrey"), axis.title.x=element_blank(),
        panel.background = element_rect(fill = "white"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.position = c(0.2,0.70),
        legend.key.size = unit(1.5,"line")) + 
  guides(colour = guide_legend(override.aes = list(size = 3)), shape = guide_legend(override.aes = list(size = 5))) +
  scale_size(guide = "none")

但是,我希望将6月19日的日期标签替换为一个文本标签,上面写着“更改”。有没有一种方法可以在不单独指定所有标签的情况下执行此操作

我尝试使用以下代码生成一个向量,该向量可用作
scale\u x\u date
函数中的
labels
参数:

x_labels <- (as.Date(unique(meltdf2$Month)))
x_labels <- sort(x_labels)
x_labels <- c(x_labels,"Change")

x_标签由于示例不完全可复制,我用虚构的数据创建了一个响应

请注意,我必须为分析中的期间(
period
)创建一个新列,以便它是一个因子,并在其中添加了名称“Change”

后来,我根据感兴趣的日期(
dates
),使用
forcats
,function
fct\u reorder
)对级别进行了重新排序

library(dplyr)
library(lubridate)
library(ggplot2)
library(forcats)
set.seed(1)
Period_x <- data.frame(Period = seq(lubridate::mdy('jan-01-2018'),
                                    lubridate::mdy('may-01-2019'), 
                                    by = 'month'))

Period_x <- Period_x %>% 
  dplyr::mutate(Period = format(Period, '%b-%y'))
Period_x <- rbind(Period_x, "Change")

Period2 <- seq(lubridate::mdy('jan-01-2018'),
               lubridate::mdy('jun-01-2019'), 
               by = 'month')

Period_x <- Period_x %>% 
  dplyr::mutate(Dates = Period2,
                y = rnorm(18), 
                Period = as.factor(Period),
                Period = forcats::fct_reorder(Period, 
                                              Dates))

ggplot2::ggplot(Period_x) +
  geom_point(aes(x = Period, y = y))
库(dplyr)
图书馆(lubridate)
图书馆(GG2)
图书馆(供猫用)
种子(1)
你能在
meltdf2
中给出
dput()
周期吗?