r - 在条形图中的主网格线之间添加网格线

标签 r plotly

我想在图表的主网格线之间添加四条颜色更浅的线,如下例所示。我如何指定它们?

enter image description here

library(plotly)
co<-c("AL","FG","GH","HJ","FT")
va<-c(1000000,1000000,1000000,5000000,5000000)
cova<-data.frame(co,va)

fig1 <- plot_ly(cova, x = ~co, y = ~va,
                type = 'bar'
                
                
                
                
) 
fig1%>% layout(showlegend=F,
               font = list(color = '#a2a2a2'),
               yaxis = list(title="",
                            showgrid = T,gridcolor = "#a2a2a2", showline = FALSE, showticklabels = TRUE, domain= c(0, 0.85)),
               xaxis = list(title="",zeroline = FALSE, showline = T,showticklabels = F,tickangle=45, showgrid = FALSE))
  

最佳答案

已更新

答案已在 SO major and minor tick marks 上提供。

  • 适应使用条形图
  • 进行调整,以便第二条轨迹不会在与主要网格线相同的位置创建网格线
library(plotly)
library(comprehenr)

co<-c("AL","FG","GH","HJ","FT")
va<-c(1000000,1000000,1000000,5000000,5000000)
cova<-data.frame(co,va)

fig1 <- plot_ly(cova, x = ~co, y = ~va,
                type = 'bar'
) 
fig1 <- fig1%>% layout(showlegend=F,
               font = list(color = '#a2a2a2'),
               yaxis = list(title="", showgrid = T,gridcolor = "#a2a2a2", showline = FALSE, showticklabels = TRUE, 
                            domain= c(0, 0.85)),
               xaxis = list(title="",zeroline = FALSE, showline = T,showticklabels = F,tickangle=45, showgrid = FALSE))

# plot additional transpareent bar on top of original bar
# give this "invisible" layer a different y-axis with ticks which are minor ticks
# this then generates gridlines,  that can be styles as you please
# NB - tickvals excludes major tickval using integer remainder logic in list comprehension
mtick <- 2*10**5 # minor ticks every 200k
fig1 %>% add_trace(x=~co, y=~va, yaxis="y2", marker=list(color='rgba(0,0,0,0)')) %>%
    layout(yaxis2=list( overlaying="y", 
                       tickvals=to_list(for(i in seq(0, max(cova$va), by=mtick) )  if (i%%10**6) i ), 
                           showticklabels=FALSE ))

enter image description here

问题答案的上一次迭代

已创建R环境。如前所述,pythonr 中的解决方案相同。只是语言语法糖来区分它们。

  • 在每个tickval上绘制一条网格线
  • ticktext 控制轴中的值。因此 "" 您想要一条网格线,但没有刻度值。注意空字符串会产生奇怪的行为,使得图形改变大小
import plotly.graph_objects as go
import pandas as pd
import requests, io

dfall = pd.read_csv(io.StringIO(
    requests.get("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv").text))
dfall["date"] = pd.to_datetime(dfall["date"])
df_g = (dfall.loc[dfall.date.dt.month.ge(12) & dfall.date.dt.year.eq(2020) & dfall.continent.eq("Europe")]
 .groupby(["iso_code","location"]).agg({"new_deaths":"sum"})
 .sort_values("new_deaths", ascending=False)
 .head(8)
)

fig = go.Figure(data=[go.Bar(x=df_g.index.get_level_values("location"), y=df_g[c], 
                       marker={"color":df_g[c], "colorscale":"viridis"}) 
                for c in df_g.columns],
         layout={"title":"Total number of deaths in last month of 2020",
                "paper_bgcolor":'rgba(0,0,0,0)', "plot_bgcolor":'rgba(0,0,0,0)'})

fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='Purple', 
                 tickvals=[t for t in range(0, 20001, 1000)],
                 ticktext=[t/1000 if (t%5000)==0 else " " for t in range(0, 20001, 1000)]
                )
fig.update_xaxes(showline=True, linewidth=2, linecolor='black')


r

library(data.table)
library(plotly)
library(comprehenr)

dfall <- fread("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv")
df_g <- dfall[date %between% c("2020-12-01", "2020-12-31") & continent=="Europe", sum(new_deaths), by=location][order(-V1)][1:8] 
setnames(df_g, old="V1", new="new_deaths")

p <- plot_ly(df_g, x=~location, y=~new_deaths, type="bar", color=~new_deaths, colors="viridis") %>%
    layout(yaxis=list(showgrid=TRUE, 
                      gridwidth=1, 
                      gridcolor='Purple', 
                      ticktext=to_list(for(t in 0:20) if (t%%5!=0) " " else t),
                      tickvals=c(0:20)*1000),
          xaxis=list(showline=TRUE, linewidth=2, linecolor='black'))


p

关于r - 在条形图中的主网格线之间添加网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67448823/

相关文章:

python - 通过回调更新 Dash 数据表

plotly :如何在我的桑基图列上写文本?

R 在空行上拆分文本

r - data.table() 仍然将字符串转换为因子?

r - 如何将前一个表达式的结果赋给变量?

Python Plotly - 多个下拉图,每个都有子图

r - 如何使用ggplotly创建多色分段线?

python - Plotly:如何使子图的 x 和 y 轴标题变大?

r - ggplot2:使用 stat_smooth 时的透明图例背景

R Shiny : Using a Vector for Display and another Vector for Value in Choice List