Didzis Elferts展示了如何使用ggplot2和ggdendro绘制树形图:
这是代码:
labs = paste("sta_",1:50,sep="") #new labels
rownames(USArrests)<-labs #set new row names
hc <- hclust(dist(USArrests), "ave")
library(ggplot2)
library(ggdendro)
#convert cluster object to use with ggplot
dendr <- dendro_data(hc, type="rectangle")
#your own labels are supplied in geom_text() and label=label
ggplot() +
geom_segment(data=segment(dendr), aes(x=x, y=y, xend=xend, yend=yend)) +
geom_text(data=label(dendr), aes(x=x, y=y, label=label, hjust=0), size=3) +
coord_flip() + scale_y_reverse(expand=c(0.2, 0)) +
theme(axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank(),
axis.title.y=element_blank(),
panel.background=element_rect(fill="white"),
panel.grid=element_blank())
Run Code Online (Sandbox Code Playgroud)
有谁知道,如何着色不同的集群?例如,您希望将2个群集(k = 2)着色?
我有以下数据帧:
Data <- data.frame(
date = c("2001-01-01", "2001-02-01", "2001-03-01", "2001-04-01", "2001-05-01", "2001-06-01"),
qtr = c("NA", "NA","NA","NA","NA","NA")
)
Run Code Online (Sandbox Code Playgroud)
我想用Year/Quater填写Data $ qtr - fe 01/01(我需要这种格式!).
我写了一个函数:
fun <- function(x) {
if(x == "2001-01-01" | x == "2001-02-01" | x == "2001-03-01") y <- "01/01"
if(x == "2001-04-01" | x == "2001-05-01" | x == "2001-06-01") y <- "01/02"
return(y)
}
n$qtr <- sapply(n$date, fun)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我总是收到错误消息:
Error in FUN(X[[1L]], ...) : Object 'y' not found
Run Code Online (Sandbox Code Playgroud)
为什么?
我有来自twitter的推文语料库.我清理这个语料库(removeWords,tolower,删除URls),最后还想删除标点符号.
这是我的代码:
tweetCorpus <- tm_map(tweetCorpus, removePunctuation, preserve_intra_word_dashes = TRUE)
Run Code Online (Sandbox Code Playgroud)
现在的问题是,通过这样做,我也松开了#标签.有没有办法用tm_map删除标点符号但保留标签?
我有以下数据集:
Data <- data.frame(
week = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
variable = c("nuclear", "nuclear", "nuclear", "nuclear", "atomic", "atomic", "atomic", "atomic", "unemployment", "unemployment", "unemployment", "unemployment"),
value = c(2, 3, 1, 4, 5, 1, 2, 3, 3, 2, 5, 1)
)
Run Code Online (Sandbox Code Playgroud)
我需要一张黑白图表.所以我使用以下代码:
ggplot(Data, aes(week, value, group=variable)) + xlab("Year") + ylab("Frequency of searches") +
geom_line(aes(linetype=variable))+ # Line type depends on cond
geom_point(aes(shape=variable))+ # Shape depends on cond
scale_shape_manual(values=c(0,1,2)) + # Change shapes
scale_linetype_manual(values=c("solid", "dotted", "dotdash"))+ …Run Code Online (Sandbox Code Playgroud) 我有一个包含超过20.000个观测值的数据集,基本上看起来像这样:
df <- data.frame(
user = c("ABC", "DEF", "GHI"),
location = c("Chicago, the windy city", "Oxford University", "Paris")
)
Run Code Online (Sandbox Code Playgroud)
我要添加其他三列city,long,lat并填写这些列与该城市名称,而geolocations(经度和纬度).
因此我想使用maps包及其world.cities数据库:
library(maps)
data(world.cities)
Run Code Online (Sandbox Code Playgroud)
如果城市名称以location正确的方式显示,则添加城市名称和地理位置并不困难.然而,他们中的大多数确实有其他字符串(例如"芝加哥,多风的城市").
如何根据world.cities数据库提取城市名称,并将真实的城市名称写入列city和地理定位到long和lat?
基本上,应该可以将 html 代码直接复制到 Quarto 文档 (.qmd) 中,然后将其呈现到 html 网站。
我尝试这样做,并将以下代码从Simplex 主题复制到四开网站:
<div class="card border-primary mb-3" style="max-width: 20rem;">
<div class="card-header">Header</div>
<div class="card-body">
<h4 class="card-title">Primary card title</h4>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
</div>
<div class="card border-secondary mb-3" style="max-width: 20rem;">
<div class="card-header">Header</div>
<div class="card-body">
<h4 class="card-title">Secondary card title</h4>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of …Run Code Online (Sandbox Code Playgroud) 我有一个日期为角色的矢量
x <- "2015-02-01 09:05:23"
Run Code Online (Sandbox Code Playgroud)
我想将其转换为dateTime对象
x <- as.POSIXct(strptime(x, "%Y-%m-%d %H:%M:%S"), tz = "GMT")
Run Code Online (Sandbox Code Playgroud)
然后"T"作为日期和时间之间的分隔符(请参阅XML Schema)以获得以下输出
"2015-02-01T09:05:23"
Run Code Online (Sandbox Code Playgroud)
如何将"T"分隔符放入字符串?
r ×6
ggplot2 ×2
bootswatch ×1
date ×1
datetime ×1
geolocation ×1
ggdendro ×1
html ×1
maps ×1
punctuation ×1
quarto ×1
regex ×1
tm ×1
weekday ×1