R绘图x轴字符/系数(组合编号和-)。仅显示仅包含数字的轴的绘图。缺陷
我正在尝试使用R绘图x轴字符/系数(组合编号和-)。仅显示仅包含数字的轴的绘图。缺陷,r,plotly,R,Plotly,我正在尝试使用plotly以字符串(组合号)作为x轴绘制条形图。 (“1”、“2”、“3”、“4-5”、“6-8”、“9-13”、“14-21”、“22-34”、“35-55”) 但是,仅绘制了3个数据。(“1”、“2”、“3”) 代码如下: library(plotly) dfb = structure(groups = c("1", "2", "3", "4 - 5", "6 - 8", "9 - 13", "14 - 21", "22 -
plotly
以字符串(组合号)作为x轴绘制条形图。(“1”、“2”、“3”、“4-5”、“6-8”、“9-13”、“14-21”、“22-34”、“35-55”) 但是,仅绘制了3个数据。(“1”、“2”、“3”) 代码如下:
library(plotly)
dfb = structure(groups = c("1", "2", "3", "4 - 5", "6 - 8",
"9 - 13", "14 - 21", "22 - 34", "35 - 55"),
counts = c(29090,10074, 4573, 4029, 2289, 1120, 337, 78, 15)),
class = c("data.frame"),
row.names = c(NA,-9L),
.Names = c("groups","counts")
)
plot_ly(dfb,
x = groups,
y = counts,
type = "bar")
返回:
但如果我对只包含数字的组进行筛选,则效果良好:
dfc=subset(dfb,dfb$groups!='1')
plot_ly(dfc,
x = groups,
y = counts,
type = "bar")
为什么会这样?
如何解决这个问题
我之所以使用plotly,是因为我想将它与Shiny一起使用,目前它最适合我
我没有使用ggplotly(生成ggplot,然后将其转换为plotly),因为有时轴标题会被截断。恐怕这是一个重复的问题:。但是,这里有两种解决方案: 解决方案1(也在提及的问题中提出): 解决方案2(不那么优雅)
groups\u str
library(plotly)
dfb = data.frame(groups= c("1", "2", "3", "4 - 5", "6 - 8","9 - 13", "14 - 21", "22 - 34", "35 - 55"),
counts=c(29090,10074, 4573, 4029, 2289, 1120, 337, 78, 15))
plot_ly(dfb,
x = groups,
y = counts,
type = "bar") %>%
layout(xaxis=list(type='category'))
groups_str <- c("1", "2", "3", "4 - 5", "6 - 8","9 - 13", "14 - 21", "22 - 34", "35 - 55")
groups <- 1:length(groups_str)
dfb = data.frame(groups= groups,
counts=c(29090,10074, 4573, 4029, 2289, 1120, 337, 78, 15))
plot_ly(dfb,
x = groups,
y = counts,
type = "bar") %>%
layout(xaxis=list(tickmode='array', tickvals=groups, ticktext=groups_str))