2017-08-23 380 views
0

我有以下數據集:GGPLOT2刪除軸標籤

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

+0

你見過這些類似的帖子[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)? – Ashish

+0

是的,它不工作在我的情況。 – Prometheus

+0

你的問題源於調用'theme_minimal()'AFTER修改與主題(axis.text = ...)主題'。由於'theme_minimal()'是一個完整的主題,它會覆蓋您的修改。任何時候你想修改一個情節的主題,在你的電話中任何完整的主題之後放置這個語句。 – Brian

回答

1

要刪除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

+0

它不起作用。是否有可能需要刪除標籤兩次,包括geom_point和geom_line?如果是這樣,任何想法如何? – Prometheus

+0

@Prometheus可以爲錯誤提供一個最小可重現的例子嗎? – Ashish

+0

@Ashish我剛剛更新了OP的情節。我沒有得到函數中的特定錯誤,只是結果不是我所期望的。正如我在上一篇文章中提到的,我認爲標籤問題與使用geom_point和geom_line有關。隨着axis.text.x = element_blank()我probubally刪除只是其中一個標籤。不知道如何爲第二個做。 – Prometheus

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

我認爲這清楚地回答你的問題。