小编fed*_*r80的帖子

用ggplot2在树状图中着色簇

Didzis Elferts展示了如何使用ggplot2和ggdendro绘制树形图:

R中的水平树状图与标签

这是代码:

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)着色?

r ggplot2 ggdendro

18
推荐指数
4
解决办法
1万
查看次数

格式化日期为年/季

我有以下数据帧:

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)

为什么?

r date

16
推荐指数
5
解决办法
5万
查看次数

tm自定义removePunctuation除了标签

我有来自twitter的推文语料库.我清理这个语料库(removeWords,tolower,删除URls),最后还想删除标点符号.

这是我的代码:

tweetCorpus <- tm_map(tweetCorpus, removePunctuation, preserve_intra_word_dashes = TRUE)
Run Code Online (Sandbox Code Playgroud)

现在的问题是,通过这样做,我也松开了#标签.有没有办法用tm_map删除标点符号但保留标签?

customization text-processing r punctuation tm

5
推荐指数
2
解决办法
4127
查看次数

在ggplot2中绘制每个第二个数据点

我有以下数据集:

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)

r ggplot2

2
推荐指数
2
解决办法
4323
查看次数

将城市名称和地理位置数据添加到数据框

我有一个包含超过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和地理定位到longlat

regex maps r geolocation

2
推荐指数
1
解决办法
231
查看次数

如何向 Quarto 网站添加 HTML 代码?

基本上,应该可以将 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)

html bootswatch quarto

2
推荐指数
1
解决办法
3352
查看次数

将分隔符添加到dateTime

我有一个日期为角色的矢量

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"分隔符放入字符串?

datetime r weekday

-3
推荐指数
1
解决办法
237
查看次数