R 减号不';将鼠标悬停在“打印输出”上时不显示
我使用R 减号不';将鼠标悬停在“打印输出”上时不显示,r,ggplot2,plotly,R,Ggplot2,Plotly,我使用plotly在R中创建了以下绘图。当我将光标悬停在负值的条上时,由于某种奇怪的原因(没有减号),相应的百分比标签为正值。以前是否有人遇到过类似问题/解决方法 这是一个可复制的数据集 structure(list(Date = structure(c(17956, 18048, 18140, 18231, 18322, 18414, 17956, 18048, 18140, 18231, 18322, 18414), class = "Date"), Key
plotly
在R中创建了以下绘图。当我将光标悬停在负值的条上时,由于某种奇怪的原因(没有减号),相应的百分比标签为正值。以前是否有人遇到过类似问题/解决方法
这是一个可复制的数据集
structure(list(Date = structure(c(17956, 18048, 18140, 18231,
18322, 18414, 17956, 18048, 18140, 18231, 18322, 18414), class = "Date"),
Key = c("Annual", "Annual", "Annual", "Annual", "Annual",
"Annual", "Annualised", "Annualised", "Annualised", "Annualised",
"Annualised", "Annualised"), Percent = c(0.05, 1, 0.14, -0.57,
-0.22, -17.21, -3.18, 3.31, -0.84, -1.44, -1.81, -51.05)), row.names = c(NA,
-12L), key = structure(list(Key = c("Annual", "Annualised"),
.rows = structure(list(1:6, 7:12), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), index = structure("Date", ordered = TRUE), index2 = "Date", interval = structure(list(
year = 0, quarter = 0, month = 0, week = 0, day = 1, hour = 0,
minute = 0, second = 0, millisecond = 0, microsecond = 0,
nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval",
"vctrs_rcrd", "vctrs_vctr")), class = c("tbl_ts", "tbl_df", "tbl",
"data.frame"))
下面是我的代码
#Load Packages
pacman::p_load(tidyverse,lubridate,plotly)
# Set Margin spacing for Plotly
m <- list(
t = 50,
b = 30,
pad = 4
)
# ggplot
p1 <- ggplot(dataset, aes(Date, Percent, fill = Key)) +
geom_bar(position='dodge', stat='identity') +
theme_bw() +
labs(y = '%') +
geom_hline(yintercept = 0, linetype = 'dashed',col = 'red') +
scale_fill_manual(values = c('#EFBE93','#75002B')) +
theme(legend.title = element_blank()) +
scale_x_date(date_breaks="3 months", date_labels = "%b-%Y")
## ggplotly
p1 %>% ggplotly(tooltip = c('Date','Percent')) %>%
layout(legend = list(orientation = "h", x = 0.35, y = -0.2)) %>%
add_annotations(xref='paper',yref='paper',x=1,y=-0.15,text='*Source - Stats SA',showarrow=F,
font=list(size=10,color='black')) %>%
layout(title = list(text = paste0('Quarterly Growth in South African GDP',
'<br>',
'<sup>',
'There is a sharp contrast between annual and annualised rates recorded for Q2 of this year.',
'</sup>'),x=0.07),margin = m)
#加载包
pacman::p_载荷(tidyverse、lubridate、plotly)
#设置绘图页边距
m%
布局(图例=列表(方向=“h”,x=0.35,y=-0.2))%>%
添加注释(xref='paper',yref='paper',x=1,y=-0.15,text='*Source-Stats SA',showarrow=F,
font=列表(大小=10,颜色='黑色')%>%
布局(标题=列表(文本=粘贴0(“南非GDP的季度增长”),
“
”,
'',
“今年第二季度记录的年率和年率形成了鲜明对比。”,
''),x=0.07),裕度=m)
显然这是ggplotly()中的一个bug
。见和。我找不到任何明确的解决办法
我能想到的唯一解决方案是直接使用
plotly
显式创建绘图,而不是使用ggplot
-->ggplotly()
显然这是ggplotly()
中的一个错误。见和。我找不到任何明确的解决办法
我能想到的唯一解决方案是直接使用
plotly
显式创建绘图,而不是ggplot
-->ggplotly()
,正如@eduardokapp所说,这看起来像ggplotly
中的一个bug。解决方法是添加另一份要在工具提示中使用的百分比
:
dataset$Pct <- dataset$Percent
p1 <- ggplot(dataset, aes(Date, Pct, fill = Key, Percent = Percent)) +
geom_bar(position='dodge', stat='identity') +
theme_bw() +
labs(y = '%') +
geom_hline(yintercept = 0, linetype = 'dashed',col = 'red') +
scale_fill_manual(values = c('#EFBE93','#75002B')) +
theme(legend.title = element_blank()) +
scale_x_date(date_breaks="3 months", date_labels = "%b-%Y")
p1 %>% ggplotly(tooltip = c('Date','Percent')) %>%
layout(legend = list(orientation = "h", x = 0.35, y = -0.2)) %>%
add_annotations(xref='paper',yref='paper',x=1,y=-0.15,text='*Source - Stats SA',showarrow=F,
font=list(size=10,color='black')) %>%
layout(title = list(text = paste0('Quarterly Growth in South African GDP',
'<br>',
'<sup>',
'There is a sharp contrast between annual and annualised rates recorded for Q2 of this year.',
'</sup>'),x=0.07),margin = m)
数据集$Pct%
布局(图例=列表(方向=“h”,x=0.35,y=-0.2))%>%
添加注释(xref='paper',yref='paper',x=1,y=-0.15,text='*Source-Stats SA',showarrow=F,
font=列表(大小=10,颜色='黑色')%>%
布局(标题=列表(文本=粘贴0(“南非GDP的季度增长”),
“
”,
'',
“今年第二季度记录的年率和年率形成了鲜明对比。”,
''),x=0.07),裕度=m)
正如@eduardokapp所说,这看起来像是ggplotly中的一个bug。解决方法是添加另一份要在工具提示中使用的百分比
:
dataset$Pct <- dataset$Percent
p1 <- ggplot(dataset, aes(Date, Pct, fill = Key, Percent = Percent)) +
geom_bar(position='dodge', stat='identity') +
theme_bw() +
labs(y = '%') +
geom_hline(yintercept = 0, linetype = 'dashed',col = 'red') +
scale_fill_manual(values = c('#EFBE93','#75002B')) +
theme(legend.title = element_blank()) +
scale_x_date(date_breaks="3 months", date_labels = "%b-%Y")
p1 %>% ggplotly(tooltip = c('Date','Percent')) %>%
layout(legend = list(orientation = "h", x = 0.35, y = -0.2)) %>%
add_annotations(xref='paper',yref='paper',x=1,y=-0.15,text='*Source - Stats SA',showarrow=F,
font=list(size=10,color='black')) %>%
layout(title = list(text = paste0('Quarterly Growth in South African GDP',
'<br>',
'<sup>',
'There is a sharp contrast between annual and annualised rates recorded for Q2 of this year.',
'</sup>'),x=0.07),margin = m)
数据集$Pct%
布局(图例=列表(方向=“h”,x=0.35,y=-0.2))%>%
添加注释(xref='paper',yref='paper',x=1,y=-0.15,text='*Source-Stats SA',showarrow=F,
font=列表(大小=10,颜色='黑色')%>%
布局(标题=列表(文本=粘贴0(“南非GDP的季度增长”),
“
”,
'',
“今年第二季度记录的年率和年率形成了鲜明对比。”,
''),x=0.07),裕度=m)
谢谢@eduardokapp!我现在使用@user2554330的解决方案,但从现在起,我可能只在plotly
中绘制绘图!谢谢@eduardokapp!我现在使用@user2554330的解决方案,但从现在起,我可能只在plotly
中绘制绘图!