在R:格式轴中绘图 - 将标签刻度为百分比

时间:2017-06-19 19:38:01

标签: r plot format plotly axis-labels

我在plotly创建条形图,y轴代表0-1内的百分比或份额。 y轴显示为0.05而不是5.0%。

有没有办法将y轴刻度标签显示为%##

我尝试使用tickformat = "%",但似乎无法正常使用。

2 个答案:

答案 0 :(得分:26)

您可以使用plotly

layout中执行此操作
p <- p %>% 
          layout(yaxis = list(tickformat = "%"))

或者,如果您只想添加%并且不重新格式化数字,那么:

p <- p %>% 
          layout(yaxis = list(ticksuffix = "%"))

实施例

这是一个示例,显示如何根据需要编辑y轴刻度。 (比如乘以数字并添加后缀等)

我有这段代码和下图:

p <- plot_ly(z = eegmean$value, 
             x = eegmean$xproj, y= eegmean$yproj, type = "contour") %>% 
                 layout(yaxis = list(range = c(0, 1)))

enter image description here

然后我修改如下,这给了我想要的输出:

g <- plot_ly(z = eegmean$value, 
             x = eegmean$xproj, y= eegmean$yproj*100, type = "contour") %>% 
                 layout(yaxis = list(range = c(0, 100)))%>% 
                 layout(yaxis = list(ticksuffix = "%"))

enter image description here

数据:

eegmean <-    
          structure(list(xproj = c(-4.36892281283989, 4.35956894475236, 
-3.66712823067503, 3.66912002532953, -6.74087785458615, 6.7287326256584, 
-3.06883681930631, 3.0727815538517, -3.05334720378955, 3.0570879596344, 
-3.79278629306119, 3.79086730312228, -7.07653326595358, 7.06235689946147, 
-7.90472265899708, 7.886291820964), yproj = c(0.0590663494057822, 
0.0624572214558794, 4.86096691858553, 4.85057791325599, 5.19791938823655, 
5.18984777332146, 9.40308855517187, 9.39510236056629, -9.35605694441838, 
-9.34632728162916, -4.81178659276704, -4.80386416586077, -5.3889955653921, 
-5.37981449730605, -0.00583969391994209, -0.00704057111565196
), value = c(0.0606980290462218, 0.0608382874925463, 0.0517195368020531, 
0.0531772440361526, 0.0204264049886253, 0.0177325467223879, 0.0392064861131087, 
0.0425640060844722, 0.0788962178010734, 0.0740093285228833, 0.0749098131481143, 
0.0759725415557911, 0.0688015959610801, 0.0762816652838652, 0.0548817124454006, 
0.0646901969995537)), .Names = c("xproj", "yproj", "value"), row.names = c("C3", 
"C4", "F3", "F4", "F7", "F8", "FP1", "FP2", "O1", "O2", "P3", 
"P4", "P7", "P8", "T7", "T8"), class = "data.frame")

答案 1 :(得分:9)

plotly documentation会将我们引导至this页面,其中包含可用格式选项的完整列表。

在这种情况下,格式化5%tickformat='%'等百分比就足够了。如果你想显示小数点,那么像'.n%'这样的东西就可以了(用小数点后面的数字替换n)。

上面引用的github页面包含更多格式化选项。干杯!

相关问题