Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用工具提示或悬停显示总计_R_Ggplot2_Plotly_Ggplotly - Fatal编程技术网

R 使用工具提示或悬停显示总计

R 使用工具提示或悬停显示总计,r,ggplot2,plotly,ggplotly,R,Ggplot2,Plotly,Ggplotly,在这段代码的帮助下,我可以得到每个条的总数,但是如何使用悬停或工具提示功能来显示这个总数呢 #Data hp=read.csv(textConnection( "class,year,amount a,99,100 a,100,200 a,101,150 b,100,50 b,101,100 c,102,70 c,102,80 c,103,90 c,104,50 d,102,90&quo

在这段代码的帮助下,我可以得到每个条的总数,但是如何使用悬停或工具提示功能来显示这个总数呢

#Data
    hp=read.csv(textConnection(
      "class,year,amount
    a,99,100
    a,100,200
    a,101,150
    b,100,50
    b,101,100
    c,102,70
    c,102,80
    c,103,90
    c,104,50
    d,102,90"))
    hp$year=as.factor(hp$year)
   

d = ggplot(hp, aes(reorder(class, -amount, sum), amount, fill = year)) +
  geom_col() 
  #geom_text(aes(label = stat(y), group = class),stat = 'summary', fun = sum,vjust = -1)

ggplotly(d)

您可以在数据中为每个
类别创建一列
总和
金额的

library(dplyr)
library(ggplot2)
library(plotly)

plot1 <- hp %>%
  group_by(class, year) %>%
  summarise(amount = sum(amount)) %>%
  mutate(total_sum = sum(amount)) %>%
  ggplot(aes(class, amount, fill = year, text = total_sum)) +
  geom_col() +
  geom_text(aes(label = amount), vjust = -1, 
            position = position_stack(vjust = .5))


ggplotly(plot1, tooltip = 'text')
库(dplyr)
图书馆(GG2)
图书馆(绘本)
1%
分组依据(类别、年份)%>%
汇总(金额=总和(金额))%>%
突变(总数=总和(金额))%>%
ggplot(不良事件(类别、金额、填充=年份、文本=总金额))+
geom_col()+
geom_文本(aes(标签=金额),vjust=-1,
位置=位置\堆栈(vjust=.5))
ggplotly(plot1,工具提示='text')

您在问题上添加了
ggplotly
,但似乎根本没有使用该软件包?你试过什么了吗?基本ggplot2不允许悬停交互,但应使用
ggplotly
。这个现有的问题可能会有所帮助:我试过了,但它没有显示@MrFlick条的总数