ggplot2删除轴标签

时间:2017-08-23 08:41:25

标签: r ggplot2 plotly ggplotly

我有以下数据集:

dput(head(active_clients))
structure(list(Date = structure(c(1422662400, 1425081600, 1427760000, 
1430352000, 1433030400, 1435622400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), value = c(65139, 66615, 66669, 67081, 67277, 
67366), month = 1:6, year = c(2015L, 2015L, 2015L, 2015L, 2015L, 
2015L), year_month = c("1/15", "2/15", "3/15", "4/15", "5/15", 
"6/15"), year2 = c("15", "15", "15", "15", "15", "15")), .Names = c("Date", 
"value", "month", "year", "year_month", "year2"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

我正在使用ggplot2绘制以下线/点图。

t <- ggplot(active_clients)  +
  geom_point(aes(as.factor(year_month), 
                 value), 
             size = 2, 
             color="deepskyblue4") +
  geom_line(aes(as.factor(year_month), 
                value,
                group = 1, alpha = 0.5), 
            color = "deepskyblue4") +

  xlab("") + 
  ylab("") +
  theme(legend.title = element_blank()) +
  theme_minimal()

ggplotly(t)

但我无法从x轴上删除标签。

我还尝试添加:

theme(legend.title = element_blank(), axis.text = element_blank())

不确定我做错了什么。

任何提示?

更新

这是我得到的情节:

enter image description here

2 个答案:

答案 0 :(得分:6)

要删除x轴标签,您应该尝试在axis.text.x=element_blank()

中使用theme()

删除x轴标签:

ggplot(active_clients)  +
  geom_point(aes(as.factor(year_month), value), size = 2, color="deepskyblue4") +
  geom_line(aes(as.factor(year_month), value, group = 1, alpha = 0.5), color = "deepskyblue4") +
  theme_minimal()+
  theme(axis.text.x=element_blank())

enter image description here

答案 1 :(得分:1)

如何删除x轴标签并将其保存为ggplot对象。此后,将它包裹在ggplotly周围,它应该做的伎俩。最小可重复的例子如下;

library(plotly)
library(ggplot2)
# create a ggplot object and remove the x-axis label
bp <- ggplot(PlantGrowth, aes(x=group, y=weight)) + 
  geom_boxplot()+
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(),
                      axis.ticks.x=element_blank())
# show the ggplot
bp
# create a ggplotly object from the ggplot object
p<- ggplotly(bp)
# show the ggplotly object
p

enter image description here

我认为这清楚地回答了你的问题。