14

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:

enter image description here

Prradep
  • 5,506
  • 5
  • 43
  • 84
Prometheus
  • 1,977
  • 3
  • 30
  • 57
  • Have you seen these similar posts [1](https://stackoverflow.com/questions/35090883/remove-all-of-x-axis-labels-in-ggplot), [2](https://stackoverflow.com/questions/6528180/ggplot2-plot-without-axes-legends-etc)? – mnm Aug 23 '17 at 08:57
  • yes, its not working working in my case. – Prometheus Aug 23 '17 at 08:58
  • 2
    Your problem stems from calling `theme_minimal()` AFTER modifying the theme with `theme(axis.text = ...)`. Since `theme_minimal()` is a complete theme, it overwrites your modification. Any time you want to modify a plot's theme, put that statement after any complete themes in your call. – Brian Aug 23 '17 at 15:59

3 Answers3

15

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

enter image description here

Prradep
  • 5,506
  • 5
  • 43
  • 84
  • it's not working. Is it possible that I need to remove the labels twice, both for geom_point and geom_line? And if so, any idea how? – Prometheus Aug 23 '17 at 08:56
  • @Prometheus can you provide a minimum reproducible example for the error? – mnm Aug 23 '17 at 08:58
  • @Ashish I just updated the OP with the plot. I'm not getting a specific error in the function, it's just that the result is not what I'm expecting. As I mentioned in the previous post, I believe the labeling issue has something to do with using both geom_point and geom_line. With axis.text.x=element_blank() I'm probubally removing just one of the labels. Not sure how to do it for the second one as well. – Prometheus Aug 23 '17 at 09:02
  • @Prometheus is this what you required? – Prradep Aug 23 '17 at 09:08
  • @Prometheus not sure why you need `ggplotly` to plot your graph? – mnm Aug 23 '17 at 09:17
  • @Prradep yes. Thank you! – Prometheus Aug 23 '17 at 09:25
  • 1
    @Ashish because ggplot returns static plots, with plotly you get interactive plots. I'm using the particular plot in a dashboard. – Prometheus Aug 23 '17 at 09:26
  • 1
    I'm also learning the combination of ggplotly with shiny dashboards. Good luck! – Prradep Aug 23 '17 at 09:27
  • 1
    @Prometheus, cool. I suggest that you edit the post and add the `ggplotly` tag besides the other tags. I'm learning `ggplotly` so will have to do some digging to get back to you. – mnm Aug 23 '17 at 09:30
  • @Ashish thats a good idea. I don't have enough reputaiton to create a new tag tough. Perhaps some admin/moderation can fix it. – Prometheus Aug 23 '17 at 09:34
12

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

enter image description here

I think this clearly answers your question.

mnm
  • 1,962
  • 4
  • 19
  • 46
0

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

enter image description here

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

Quinten
  • 35,235
  • 5
  • 20
  • 53