[英]How can I position tickmarks and axis labels (or ticktext) at the end of bars (rather than centered) in a bar chart using R plotly?
I am trying to create a bar chart that shows how much funds received households of certain income.我正在尝试创建一个条形图,显示特定收入家庭收到了多少资金。 For that I am using the following code:为此,我使用以下代码:
chart <- plot_ly(funds,
x = ~income_group,
y = ~FundsSent,
type = "bar")
chart <- chart %>% layout(title = '<b>Fund Distribution by Income</b>',
xaxis = list(
title = list(text='Income', standoff = 10),
ticks="outside", tickson="boundaries"),
yaxis = list(
title = list(text='Funds paid', standoff = 10)),
bargap = 0.05)
Now, with "tickson=boundaries"
I managed to get the tickmarks at the end of every bar (rather than centered), but the labels are still in the middle of the column.现在,使用"tickson=boundaries"
我设法在每个条形的末尾(而不是居中)获取刻度线,但标签仍位于列的中间。
Bar chart showing missplacement of axis labels显示轴标签错位的条形图
This tells the wrong story, as it would appear that the first column for example shows the funds paid to those with 0 income, when instead it's showing the funds paid to those with income 0 to 5,000.这说明了错误的故事,例如,第一列似乎显示支付给收入为 0 的人的资金,而它显示的是支付给收入为 0 到 5,000 的人的资金。
I have also tried setting the X axis labels by hand, using tickmode="array"
, ticktext
and setting tickvals
manually but, while that aligns the ticks with the text, it goes again to centering the tickmarks (as if the tickson="boundaries"
argument wasn't working anymore).我也尝试过手动设置 X 轴标签,使用tickmode="array"
, ticktext
并手动设置tickvals
但是,虽然将刻度与文本对齐,但它再次使刻度线居中(就像tickson="boundaries"
论点不再起作用)。
Any help for a desperate and frustrated economist?对绝望和沮丧的经济学家有什么帮助吗?
I actually just made it work using tickson="boundaries"
to set the ticks in between bars;我实际上只是使用tickson="boundaries"
来设置条形之间的刻度; setting tickvals
to half of the interval range to the left to move the ticks off the center, and ticktext
for the labels.将tickvals
设置为向左间隔范围的一半,以将刻度从中心ticktext
,并为标签设置刻度ticktext
。
The reason it wasn't working before is because I had written my tickvals
without using "" (I thought that, being numbers rather than text, I didn't need the inverted commas).之前它不起作用的原因是因为我在没有使用“”的情况下编写了我的tickvals
(我认为,作为数字而不是文本,我不需要引号)。 This is how my code looks now, and it works:这是我的代码现在的样子,它可以工作:
chart %>% layout(title = '<b>Fund Distribution by Income</b>',
xaxis = list(
title = list(text='<b>Income</b>', standoff = 10),
ticks="outside", tickson="boundaries",
ticktext=list("0", "5,000", "10,000", "15,000", "20,000"), #label
tickvals=list("-2500", "2500", "7500", "12500", "17500")) #position
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.