简体   繁体   中英

ggplot2 remove axis label

I have the following dataset:

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"))

And i'm plotting the following line/point graph with 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)

But I can't manage to remove the labels from the x - axis.

I've also tried adding:

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

Not sure what I'm doing wrong.

Any hints?

Update

This is the plot I'm getting:

在此处输入图像描述

To remove x-axis labels, you should try to use axis.text.x=element_blank() in the theme()

Removing the x-axis labels:

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())

在此输入图像描述

How about removing the x-axis label and saving it as a ggplot object. Thereafter, wrap it around ggplotly and it should do the trick. A minimum reproducible example is as follows;

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

在此输入图像描述

I think this clearly answers your question.

Since ggplotly converts your ggplot object to plotly , you could use layout with showticklabels = FALSE to remove the x-axis labels. So simply adding layout(xaxis= list(showticklabels = FALSE)) to your ggplotly is enough like this:

library(ggplot2)
library(plotly)
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) %>%
  layout(xaxis= list(showticklabels = FALSE))

在此处输入图像描述

Created on 2022-08-19 with reprex v2.0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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