23

ggvis プロットにタイトルを追加したい。どこにも例が見つかりません。他の R プロットを使用するのは簡単です。

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

明らかな何かが欠けているように感じます。どんな助けでも感謝します。

4

3 に答える 3

34

まあ、それはまだ実装されていないようです(または文書化されていませんか?)。これはすぐに追加されると確信しています。今のところ、次のような汚いハックを使用できます。

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

さらに、このハックを複数回使用したい場合は、省略形を作成できます。

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

編集:

これで、メイン タイトルと x 軸ラベルの両方を同時に設定できるようになり、後述のオーバーラップが修正されました。

于 2014-07-30T06:23:44.503 に答える
5

また、タイトルのフォント サイズを変更するには、次のコードを使用します (@tonytonov の回答から改作):

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)
             ), ...)
}
于 2015-02-05T10:30:46.360 に答える
3

デフォルトの X 軸に干渉しないように @tonytonov の回答を更新しました。これは引き続き軸として実装されますが、独自の「タイトル」スケールを実装し、フォントサイズや色などのユーザー指定のタイトル プロパティを、軸を非表示にするために必要なデフォルト プロパティと適切にマージします。このバージョンは、特定の背景色を想定せずに軸を非表示にします。関数の後に 3 つの例を示します。

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)))
于 2016-06-28T20:42:57.113 に答える