简体   繁体   中英

Add a plot title to ggvis

I want to add a title to a ggvis plot. I can't find an example anywhere. It's simple to do with other R plots, eg

library(ggplot2)
library(ggvis)
x <- 1:10
y <- x^2
df <- data.frame(x, y)

plot(x, y, main = "Plot Title")  # Base R with title
ggplot(df, aes(x, y)) + geom_point() + ggtitle("Plot Title")  # ggplot2 with title
df %>% ggvis(~x, ~y) %>% layer_points()  # ggvis but no title??

Feel like I'm missing something obvious. Any help appreciated.

Well, seems like it has not been implemented (or documented?) yet. I'm pretty sure this will be added soon. For now, you can use a dirty hack like so:

df %>% ggvis(~x, ~y) %>% layer_points() %>% 
  add_axis("x", title = "X units") %>%
  add_axis("x", orient = "top", ticks = 0, title = "Plot Title",
           properties = axis_props(
             axis = list(stroke = "white"),
             labels = list(fontSize = 0)))

Furthermore, if you would like to use this hack multiple times, you can create a shorthand for it.

add_title <- function(vis, ..., x_lab = "X units", title = "Plot Title") 
{
  add_axis(vis, "x", title = x_lab) %>% 
    add_axis("x", orient = "top", ticks = 0, title = title,
             properties = axis_props(
               axis = list(stroke = "white"),
               labels = list(fontSize = 0)
             ), ...)
}
df %>% ggvis(~x, ~y) %>% layer_points() %>% add_title() #same as before
df %>% ggvis(~x, ~y) %>% layer_points() %>% add_title(title = "Hello", x_lab = "ton")

Edit:

Now you can set both the main title and the x axis label simultaneously, fixing the overlap mentioned below.

Also, to change the font size of the title use this code (adapted from @tonytonov's answer):

add_title <- function(vis, ..., x_lab = "X units", title = "Plot Title") 
{
  add_axis(vis, "x", title = x_lab) %>% 
    add_axis("x", orient = "top", ticks = 0, title = title,
             properties = axis_props(
               axis = list(stroke = "white"),
               title = list(fontSize = 32),
               labels = list(fontSize = 0)
             ), ...)
}

I updated @tonytonov's answer to not interfere with the default X axis. It is still implemented as an axis, but implements its own 'title' scale and properly merges user-supplied title properties like fontsize and color with the default properties needed to make the axis invisible. This version hides the axis without presuming a particular background color. Three examples follow the function.

library(ggvis)

# ggvis lacks a plot title function, so add one.
# based on clever hack by tonytonov
# http://stackoverflow.com/a/25030002/1135316
add_title <- function(vis, ..., properties=NULL, title = "Plot Title") 
{
    # recursively merge lists by name
    # http://stackoverflow.com/a/13811666/1135316
    merge.lists <- function(a, b) {
        a.names <- names(a)
        b.names <- names(b)
        m.names <- sort(unique(c(a.names, b.names)))
        sapply(m.names, function(i) {
                   if (is.list(a[[i]]) & is.list(b[[i]])) merge.lists(a[[i]], b[[i]])
                   else if (i %in% b.names) b[[i]]
                   else a[[i]]
               }, simplify = FALSE)
    }

    # default properties make title 'axis' invisible
    default.props <- axis_props(
        ticks = list(strokeWidth=0),
        axis = list(strokeWidth=0),
        labels = list(fontSize = 0),
        grid = list(strokeWidth=0)
        )
    # merge the default properties with user-supplied props.
    axis.props <- do.call(axis_props, merge.lists(default.props, properties))

    # don't step on existing scales.
    vis <- scale_numeric(vis, "title", domain = c(0,1), range = 'width')
    axis <- ggvis:::create_axis('x', 'title', orient = "top",  title = title, properties = axis.props, ...)
    ggvis:::append_ggvis(vis, "axes", axis)
}

# add title with default X axis.
iris %>% 
  ggvis(x = ~Petal.Width) %>% 
  layer_points(y = ~Petal.Length, fill = ~Species) %>%
  add_title(title = "Petal.Width vs. Petal.Length")

# Add title with overridden X axis
iris %>% 
  ggvis(x = ~Petal.Width) %>% 
  layer_points(y = ~Petal.Length, fill = ~Species) %>%
  add_axis("x", title = "X-axis!!!!", title_offset=40,
           properties = axis_props(title=list(fontSize=16),
               labels = list(fontSize = 12))) %>%
  add_title(title = "Petal.Width vs. Petal.Length")

# Change the font size of the title.
iris %>% 
  ggvis(x = ~Petal.Width) %>% 
  layer_points(y = ~Petal.Length, fill = ~Species) %>%
  add_title(title = "Petal.Width vs. Petal.Length",
            properties = axis_props(title=list(fontSize=20)))

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