如何在ggplotly中添加长标题?

时间:2016-07-12 18:28:24

标签: r ggplot2 plotly

考虑以下示例:

data(canada.cities, package = "maps")

viz <- ggplot(canada.cities, aes(long, lat)) +

borders(regions = "canada") +

coord_equal() +

geom_point(aes(text = name, size = pop), colour = "red", alpha = 1/2) +

ggtitle(paste("Average bill for 1 people \n", 

"Average bill for 2 people \n", 

"Average bill for 3 people \n",

"Average bill for 4 people", sep = "")) 

ggplotly(viz)

enter image description here

标题出现在一行中,这就是它被截止的原因。 我想要的是:

Average bill for 1 people
Average bill for 2 people
Average bill for 3 people
Average bill for 4 people
标题中的

(4行)。

1 个答案:

答案 0 :(得分:2)

"<br>"

中使用HTML标记br,即ggtitle
data(canada.cities, package = "maps")

viz <- ggplot(canada.cities, aes(long, lat)) +

  borders(regions = "canada") +

  coord_equal() +


  geom_point(aes(text = name, size = pop), colour = "red", alpha = 1/2) +

  ggtitle(paste("Average bill for 1 people \n", 

                "Average bill for 2 people \n", 

                "Average bill for 3 people \n",

                "Average bill for 4 people", sep = "<br>")) 

ggplotly(viz)

输出: enter image description here