简体   繁体   中英

ggplot increasing the distance between axis labels and axis ticks

I've exhausted the documentation and previous questions on Stackoverflow but I can't seem to overcome this simple challenge.

df <- read_csv("book1.csv")

p <- ggplot(data = df, aes(x = xData, y = yData, color = colorData)) +
  geom_point(aes(text = paste("Contact:", Contact, "Number:", Number,sep="\n")), size = 1) +
  theme(legend.text=element_text(size=rel(0.6))) +
  facet_wrap(~ colorData, nrow = 4, ncol = 2) +
  xlim(0, 10) +   ylim(0, 10) + 
  labs = (x = "\nxData", y = "yData\n") 

ggplotly(p)

At the moment, my x and y labels sit on top of the ticks on all facets and I wish to extend the axis labels away from the ticks.

So far I've tried:

labs = (x = "\nxData", y = "yData\n")
xlab("\nxData") + ylab("yData\n")
theme(axis.title.x = element_text(margin = margin(t = 9, r = 0, b = 0, l = 0)))+
theme(axis.title.y = element_text(margin = margin(t = 0, r = 9, b = 0, l = 0)))
theme(axis.title.x = element_text(vjust=-0.5))+
theme(axis.title.y = element_text(hjust=-0.5))
theme(axis.title.x.bottom = element_text(margin = margin(t = 2, unit = "in")))+
theme(axis.title.y.left = element_text(margin = margin(r = 2, unit = "in")))

Nothing seems to work here. Any suggestions welcomed.

EDIT: data in copy-pasteable format:

df <- tibble::tribble(
  ~xData, ~yData, ~colorData, ~Contact, ~Number,
  3.4,    6.7,   "Dept 1",   "John",    143L,
  4.5,    7.7,   "Dept 2",   "Paul",    353L,
  6.7,    8.7,   "Dept 3",   "Mary",    232L,
  8.9,    9.9,   "Dept 4",  "Steve",    235L,
  3.4,     10,   "Dept 5",   "Bill",    124L,
  5.6,    5.4,   "Dept 6",   "Jess",    421L,
  7.6,    4.5,   "Dept 7",  "Peter",    212L
)

So it appears this may be an issue caused when calling ggploty(p) for an interactive plot but not for a regular plot.

just too long for a comment - no real answer.

Here to show how this could look like.

Try to run this code in your session and see if you have the same result. If not, then you have some weird global settings set. I'd then restart R, start a new session, and possibly also update all your packages.

library(tidyverse)
ggplot(data = mtcars) +
  geom_point(aes(mpg, disp)) +
  facet_wrap(~ carb, nrow = 4, ncol = 2) +
  theme(axis.text.x = element_text(margin = margin(t = 1, unit = "in"))) 

Created on 2021-02-08 by the reprex package (v0.3.0)

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