首页 > 解决方案 > 由于本地 rstudio 和 r ui 中的特定日语字符而无法绘制绘图,但使用 plotly 在 rstudio.cloud 中没有问题

问题描述

无法用我的 rstudio 和 Rui 用代码显示情节(查看器中的空白):

a <- c("予約","リスト")
b <- c(20,30)
df <- data.frame(a,b)
plot_ly(df, x = ~a, y = ~b,type = 'bar')

但是使用相同的代码,该图可以在 rstudio.cloud 中正确显示。当我在下面删除字符“予”时a,可以正确显示情节。

a <- c("約","リスト")
b <- c(20,30)
df <- data.frame(a,b)
plot_ly(df, x = ~a, y = ~b,type = 'bar')

我将 R 版本更改为 3.6.0(rstudio.cloud 的当前版本)但仍然无法显示。

rstudio version: 1.2.5001

先感谢您。

标签: rrstudioplotly

解决方案


据我所知,这是一个编码问题。 iconv(Japanese, to = "UTF-8")可以解决。

a <- c("予約","リスト")
b <- c(20, 30)
df <- data.frame(a, b)

plot_ly(df, x = ~ iconv(a, to = "UTF-8"), y = ~ b, type = 'bar')

# or

df2 <- data.frame(a, b, stringsAsFactors = FALSE) %>% 
  mutate(a = iconv(a, to = "UTF-8"))

plot_ly(df2, x = ~ a, y = ~ b, type = 'bar')

推荐阅读