我想使用ggplot2和来设计冲积图中各组之间的距离ggalluvial
我的示例来自https://corybrunson.github.io/ggalluvial/articles/ggalluvial.html
data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = freq,
fill = response, label = response)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", size = 3) +
theme(legend.position = "none") +
ggtitle("vaccination survey responses at three points in time")
Run Code Online (Sandbox Code Playgroud)
给我:
现在,我想缩短ms153_NSA之间的距离,同时保持ms432_NSA和ms460_NSA之间的距离相同:
我尝试使用以下命令(未成功):
https://rdrr.io/cran/ggalluvial/man/stat_flow.html
甚至谷歌也没有给我显示不对称的冲积图::'-/
我想从以下位置抓取瑞士政府为大学研究项目提供的药物信息:
http://www.spezialitaetenliste.ch/ShowPreparations.aspx?searchType=Substance&searchValue=
该页面确实提供了一个robotx.txt 文件,但是,它的内容对公众免费提供,我认为抓取这些数据是不受禁止的。
这是这个问题的更新,因为我取得了一些进展。
到目前为止我取得的成就
# opens the first results page
# opens the first link as a table at the end of the page
library("rvest")
library("dplyr")
url <- "http://www.spezialitaetenliste.ch/ShowPreparations.aspx?searchType=Substance&searchValue="
pgsession<-html_session(url)
pgform<-html_form(pgsession)[[1]]
page<-rvest:::request_POST(pgsession,url,
body=list(
`ctl00$cphContent$gvwPreparations$ctl13$gvwpPreparations$txtPageNumber`=1,
`__VIEWSTATE`=pgform$fields$`__VIEWSTATE`$value,
`__VIEWSTATEGENERATOR`=pgform$fields$`__VIEWSTATEGENERATOR`$value,
`__VIEWSTATEENCRYPTED`=pgform$fields$`__VIEWSTATEENCRYPTED`$value,
`__EVENTVALIDATION`=pgform$fields$`__EVENTVALIDATION`$value,
`ctl00$cphContent$gvwPreparations$ctl13$gvwpPreparations$ddlPageSize`="10",
`__EVENTTARGET`="ctl00$cphContent$gvwPreparations$ctl02$ctl00",
`__EVENTARGUMENT`=""
),
encode="form")
Run Code Online (Sandbox Code Playgroud)
下一篇:获取基础数据
# makes a table of all results of the first page
read_html(page) %>%
html_nodes(xpath = '//*[@id="ctl00_cphContent_gvwPreparations"]') %>%
html_table(fill=TRUE) %>%
bind_rows %>%
tibble()
Run Code Online (Sandbox Code Playgroud)
下一步:获取附加数据
# gives the desired informations (=additional data) of the …Run Code Online (Sandbox Code Playgroud) 我想使用ggplot2和修改现有的桑基图ggalluvial,使其更具吸引力
我的例子来自https://corybrunson.github.io/ggalluvial/articles/ggalluvial.html
library(ggplot2)
library(ggalluvial)
data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = freq,
fill = response, label = response)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", size = 3) +
theme(legend.position = "none") +
ggtitle("vaccination survey responses at three points in time")
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2020 年 10 月 1 日创建(v0.3.0)
现在,我想更改此图,它看起来类似于https://sciolisticramblings.wordpress.com/2018/11/23/sankey-charts-the-new-pie-chart/中的图,即 1. 更改绝对值到相对值(百分比) 2. 添加百分比标签并 3. …
我比较了三个不同组的分类数据。
我想知道是否可以轻松地将卡方检验的 p 值添加到构面 ggplots 中(因为我正在分析一个大数据集)。我刚刚读到,在比较平均值时,有一种奇妙的方法可以做到这一点https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/。但是,我找不到其他测试的解决方案(例如我的例子中的 chisq.test )。
d.test <- data.frame(
results = sample(c("A","B","C"), 30, replace =TRUE),
test = sample(c("test1", "test2","test3"), 30, replace = TRUE)
)
chisq.test(d.test$results,d.test$test)
ggplot(d.test, aes(results) ) +
geom_bar() + facet_grid(test ~ .)
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!;D
有没有办法用字符串中的单个数字替换数字范围?数字的范围可以从nn,最可能在1-15左右,也可能是4-10.
范围可以用a)表示 -
a <- "I would like to buy 1-3 cats"
Run Code Online (Sandbox Code Playgroud)
或者用词b)例如:to,bis,jusqu'à
b <- "I would like to buy 1 jusqu'à 3 cats"
Run Code Online (Sandbox Code Playgroud)
结果应该是这样的
"I would like to buy 1,2,3 cats"
Run Code Online (Sandbox Code Playgroud)
我发现这个:用一定数量替换数字范围,但在R中无法真正使用它.
我有一个手动设计的调色板pal,用于绘制使用ggplot2
pal <- c("red","red1","red2","red3","grey","darkgreen","skyblue","blue","magenta","magenta4","yellow","orange","pink","pink","black")
Run Code Online (Sandbox Code Playgroud)
我想将这些颜色转换为柔和的颜色(即 HSV 颜色空间中的高值和低饱和度)
有没有什么简单的方法可以自动做到这一点?
我pastel.colors从RPMG包中找到了,但是,此功能不会转换给定的颜色。
parse_numberreadr如果字符串包含 a 则from失败.
它适用于特殊字符。
library(readr)
#works
parse_number("%ç*%&23")
#does not work
parse_number("art. 23")
Warning: 1 parsing failure.
row col expected actual
1 -- a number .
[1] NA
attr(,"problems")
# A tibble: 1 x 4
row col expected actual
<int> <int> <chr> <chr>
1 1 NA a number .
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
更新:
预期的结果是 23
我想将特定项目作为条形图中的空条添加到 x 轴
例如:
# load packages
library(reshape2)
library(tidyverse)
library(scales)
#get data
data(tips, package = "reshape2")
tips
# make plot
myplot <- ggplot(tips, aes(day, group = sex)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
scale_y_continuous(labels=scales::percent) +
ylab("relative frequencies") +
facet_grid(~sex)
myplot
Run Code Online (Sandbox Code Playgroud)
现在我想添加缺少的工作日,作为空条:
missing_days <-c("Tue","Wed")
Run Code Online (Sandbox Code Playgroud)
如果可能,我想保留tips( 以便我仍然可以在aes命令中使用其他变量的“整洁”长数据格式。将“空”项添加到长数据格式的技巧是什么?
是否有可能为冲积土添加标签ggalluvial?
示例图:
library(ggalluvial)
data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = freq,
fill = response, label = response)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", size = 3) +
theme(legend.position = "none") +
ggtitle("vaccination survey responses at three points in time")
Run Code Online (Sandbox Code Playgroud)
现在我想将主题 id/nrs 作为标签添加到 alluvia(而不是盒子)。有可能这样做吗? 我的原始数据中每个冲积层的主题要少得多(例如 2-5)。
我有两个dataframes a和b我想结合
a <- data.frame(g=c("1","2","2","3","3","3","4","4","4","4"),h=c("1","1","2","1","2","3","1","2","3","4"))
b <- data.frame(g=c("1","2","3","3","3","4","4","4","4","4"),i=c("1","2","3","2","1","2","3","4","5","6"))
Run Code Online (Sandbox Code Playgroud)
g代表分组变量,并h和i列我想合并/加入
> a
g h
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
6 3 3
7 4 1
8 4 2
9 4 3
10 4 4
> b
g i
1 1 1
2 2 2
3 3 3
4 3 2
5 3 1
6 4 2
7 4 3
8 4 4
9 …Run Code Online (Sandbox Code Playgroud)