R-更改图表格式
] 1
我的数据看起来像屏幕捕获中所示的Excel表,我想像“ Meff_exemple”的图片一样绘制它。有人给我写了以下脚本,但行不通...
dat <- readxl::read_xlsx( "/Users/XX/Desktop/XXX.xlsx" ) |>
janitor::clean_names()
dat |>
mutate(meff = str_replace_all(meff, "not calculated", "NA")) |>
mutate(meff = as.numeric(meff)) |>
ggplot(aes(year, meff)) +
geom_col(aes(fill = land_type), position = "dodge") +
facet_wrap(~place) + theme_light(base_size = 6)
ggsave( filename = "lcc_graph_example.png", plot = last_plot(),
width = 6, height = 3, device = "png", dpi = 300 )
Error in `mutate()`: ! Problem while computing `meff =
str_replace_all(meff, "not calculated", "NA")`. Caused by error in
`stri_replace_all_regex()`: ! object 'meff' not found Run
`rlang::last_error()` to see where the error occurred.
> ggsave(
+ filename = "lcc_graph_example.png",
+ plot = last_plot(), width = 6, height = 3, device = "png", dpi = 300
+ )
> rlang::last_error() <error/dplyr:::mutate_error> Error in `mutate()`: ! Problem while computing `meff = str_replace_all(meff,
"not calculated", "NA")`. Caused by error in
`stri_replace_all_regex()`: ! object 'meff' not found
--- Backtrace:
1. ggplot2::ggplot(...)
8. stringr::str_replace_all(meff, "not calculated", "NA")
9. stringi::stri_replace_all_regex(...) Run `rlang::last_trace()` to see the full context.
]1
I have datas that look like the excel table shown in the screen capture and I want to plot it like the picture called meff_exemple. Someone wrote me the following script but it does not work...
dat <- readxl::read_xlsx( "/Users/XX/Desktop/XXX.xlsx" ) |>
janitor::clean_names()
dat |>
mutate(meff = str_replace_all(meff, "not calculated", "NA")) |>
mutate(meff = as.numeric(meff)) |>
ggplot(aes(year, meff)) +
geom_col(aes(fill = land_type), position = "dodge") +
facet_wrap(~place) + theme_light(base_size = 6)
ggsave( filename = "lcc_graph_example.png", plot = last_plot(),
width = 6, height = 3, device = "png", dpi = 300 )
Error in `mutate()`: ! Problem while computing `meff =
str_replace_all(meff, "not calculated", "NA")`. Caused by error in
`stri_replace_all_regex()`: ! object 'meff' not found Run
`rlang::last_error()` to see where the error occurred.
> ggsave(
+ filename = "lcc_graph_example.png",
+ plot = last_plot(), width = 6, height = 3, device = "png", dpi = 300
+ )
> rlang::last_error() <error/dplyr:::mutate_error> Error in `mutate()`: ! Problem while computing `meff = str_replace_all(meff,
"not calculated", "NA")`. Caused by error in
`stri_replace_all_regex()`: ! object 'meff' not found
--- Backtrace:
1. ggplot2::ggplot(...)
8. stringr::str_replace_all(meff, "not calculated", "NA")
9. stringi::stri_replace_all_regex(...) Run `rlang::last_trace()` to see the full context.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我认为使用
Janitor
sclean_names
功能您的列名称更改,因此抛出了错误。 在使用clean_names
,
因此
之后:
First I think after using
janitor
sclean_names
function your column names change and therefore the error is thrown. Therefore after usingclean_names
check your colnames before you apply it to ggplot:With the provided data, NOTE the columnnames differ from your original data:
We could do:
And we will get: