Angus Campbell
Angus Campbell

Reputation: 592

ggplot won't remove axis ticks

GGplot2 won't remove the axis ticks from my plot. I have posted the code below and the resutling image. I have tried putting theme_update() before theme_classic() but that does not appear to have an effect. I've provided the data below.

EDIT: I made the dataset smaller so it can be copy and pasted in order to reproduce the graph here

library(ggplot2)
test = read.csv("test.csv")

ggplot(test, aes(x=reorder(varnames, PercentIncMSE), 
weight=PercentIncMSE, fill=as.factor(var_categ)))+ 
  geom_bar() +
  scale_fill_discrete(name="Variable Group") +
  ylab("IncNodePurity") +
  xlab("Variable Name") +
  theme_update(axis.ticks.x = element_blank())+
  theme_classic()

enter image description here

CSV of data is here (copy and paste, save as csv and load). test.csv:

"","PercentIncMSE","varnames","var_categ"
"1",3.40165438891285,"Right Superior Temporal Gyrus S.A.","S.A."
"2",3.37949397465159,"Left Caudal Middle Frontal Gyrus S.A.","S.A."
"3",3.09930356879588,"Right Medial Temporal Lobe S.A.","S.A."
"4",3.04613121334614,"RH Mean Thickness","Thickness"
"5",2.91160219316499,"LH Mean Surface Area","S.A."
"6",2.88550323781124,"Right Medial Temporal Lobe Thickness","Thickness"
"7",2.78591569629083,"Left Precentral Gyrus S.A.","S.A."
"8",2.78561093039636,"Total Surface Area","S.A."
"9",2.65776304486951,"Right Lateral Occipital Cortex Thickness","Thickness"
"10",2.64593284649183,"Right Postcentral Gyrus Thickness","Thickness"
"11",2.62250705917071,"Right Cuneus S.A.","S.A."
"12",2.55545911837547,"Mean Thickness","Thickness"
"13",2.3676161089342,"Left Paracentral Lobule Thickness","Thickness"
"14",2.33212959759882,"Left Superior Frontal Gyrus S.A.","S.A."
"15",2.32182900208383,"Right Inferior Parietal Lobule Thickness","Thickness"
"16",2.32155274685452,"Left Posterior Cingulate Cortex S.A.","S.A."
"17",2.32132222174433,"Left Supramarginal Gyrus Thickness","Thickness"
"18",2.24006703422385,"Left Precentral Gyrus Thickness","Thickness"
"19",2.22601045517115,"Left Rostral Middle Frontal Gyrus Thickness","Thickness"
"20",2.18196099056015,"Right Superior Parietal Lobe Thickness","Thickness"

Upvotes: 2

Views: 3327

Answers (1)

Angus Campbell
Angus Campbell

Reputation: 592

Solution is here: https://www.datanovia.com/en/blog/ggplot-axis-ticks-set-and-rotate-text-labels/#:~:text=To%20remove%20a%20particular%20axis,ticks%20%3D%20element_blank())%20.

theme should contain axis.text.x = element_blank()

ggplot(test, aes(x=reorder(varnames, PercentIncMSE), weight=PercentIncMSE, 
fill=as.factor(var_categ)))+ 
  geom_bar() +
  scale_fill_discrete(name="Variable Group") +
  ylab("IncNodePurity") +
  xlab("Variable Name") +
  theme_classic() +
  theme_update(axis.ticks.x = element_blank(),
               axis.text.x = element_blank())

enter image description here

Upvotes: 2

Related Questions