我想左对齐plot.title,plot.subtitle并plot.caption在水平ggplot2条形图中.
例:
library("ggplot2") # ggplot2 2.2
df <- data.frame(type=factor(c("Brooklyn",
"Manhatten and\n Queens")),
value=c(15,30))
# manual hjust for title, subtitle & caption
myhjust <- -0.2
ggplot(df,
aes(x=type, y=value)) +
geom_bar(stat='identity') +
coord_flip() +
labs(
title = "This is a nice title",
subtitle = "A subtitle",
caption = "We even have a caption. A very long one indeed.") +
theme(axis.title=element_blank(),
plot.title=element_text(hjust = myhjust),
plot.subtitle=element_text(hjust = myhjust ),
plot.caption=element_text(hjust = myhjust))
Run Code Online (Sandbox Code Playgroud)
我该如何调整所有3个labs元素(plot.title,plot.subtitle …
我想将相应的值标签放在geom_col每个条形段中间的堆积条形图中.
但是,我天真的尝试失败了.
library(ggplot2) # Version: ggplot2 2.2
dta <- data.frame(group = c("A","A","A",
"B","B","B"),
sector = c("x","y","z",
"x","y","z"),
value = c(10,20,70,
30,20,50))
ggplot(data = dta) +
geom_col(aes(x = group, y = value, fill = sector)) +
geom_text(position="stack",
aes(x = group, y = value, label = value))
Run Code Online (Sandbox Code Playgroud)
显然,设置y=value/2为geom_text不帮助,无论是.此外,文本的顺序错误(反向).
任何(优雅的)想法如何解决这个问题?
我的数据集中有一些非常非常少的异常值使得箱图难以阅读:
library(ggplot2)
mtcars$mpg[1] <- 60
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot()
Run Code Online (Sandbox Code Playgroud)

因此,我想指出这样的极端异常值:

任何想法如何做到这一点ggplot2?转换轴不是我的选择......
在全新安装Ubuntu GNOME 16.10之后,我再也无法安装RStudio Desktop x64了.
我试图通过安装最新的*.deb文件
sudo dpkg -i ./rstudio-0.99.903-amd64.deb
Run Code Online (Sandbox Code Playgroud)
结果导致了
dpkg: dependency problems prevent configuration of rstudio
rstudio depends on libjpeg62; but:
Package libjpeg62 is not installed.
rstudio depends on libgstreamer0.10-0; but:
Package libgstreamer0.10-0 iis not installed.
rstudio depends on ibgstreamer-plugins-base0.10-0; but:
Package libgstreamer-plugins-base0.10-0 is not installed.
Run Code Online (Sandbox Code Playgroud)
我试图通过以下方式安装libgstreamer:
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
Run Code Online (Sandbox Code Playgroud)
这也导致了一个错误:
E: Package libgstreamer0.10-dev coulnd not be found. [...]
Run Code Online (Sandbox Code Playgroud)
似乎在debian上问题可以解决,但对于Ubuntu 16.10我不知道要添加到sources.list中的内容以遵循相同的指令.关于如何解决这个问题的任何想法?
我想\n在R中的一个非常长的字符向量中用换行符(\n )替换空格.但是,我不想替换每个空格,但仅当子字符串强制执行一定数量的字符(n)时.
例:
mystring <- "this string is annoyingly long and therefore I would like to insert linebreaks"
Run Code Online (Sandbox Code Playgroud)
现在我想在mystring每个子字符串长度大于20个字符(nchar > 20)的条件下在每个空格中插入换行符.
因此,结果字符串应该如下所示:
"this string is annoyingly\nlong and therefore I would\nlike to insert linebreaks")
Run Code Online (Sandbox Code Playgroud)
\n在25,26和25个字符后插入了换行符(\n ).
我怎样才能做到这一点?也许东西结合gsub和strsplit?
我想在ggplot2中使用线/条可视化两点之间的差异.
假设我们有一些关于收入和支出的数据作为时间序列.我们不仅要想象它们,还想要平衡(=收入 - 支出).此外,我们想说明余额是正数(=盈余)还是负数(=赤字).
我尝试过几种方法,但都没有产生令人满意的结果.在这里,我们使用可重复的示例.
# Load libraries and create LONG data example data.frame
library(dplyr)
library(ggplot2)
library(tidyr)
df <- data.frame(year = rep(2000:2009, times=3),
var = rep(c("income","spending","balance"), each=10),
value = c(0:9, 9:0, rep(c("deficit","surplus"), each=5)))
df
Run Code Online (Sandbox Code Playgroud)
1.了解LONG数据
不出所料,它不适用于LONG数据,因为geom_linerange参数ymin并ymax不能正确指定.ymin=value, ymax=value肯定是错误的方式(预期的行为).ymin=income, ymax=spending显然也是错误的(预期的行为).
df %>%
ggplot() +
geom_point(aes(x=year, y=value, colour=var)) +
geom_linerange(aes(x=year, ymin=value, ymax=value, colour=net))
#>Error in function_list[[i]](value) : could not find function "spread"
Run Code Online (Sandbox Code Playgroud)
2.使用WIDE数据
我差点使用WIDE数据.情节看起来很好,但geom_point(s)缺少的传说(预期的行为).简单地添加show.legend = TRUE到两个geom_point并不能解决问题,因为它会覆盖geom_linerange图例.此外,我宁愿将geom_point …
是否有一种优雅的方式ggplot2来制作geom_text/ geom_label继承theme像base_family?
或反过来问:我可以指定一个theme也适用于geom_text/ geom_label?
例:
我想text/labels看起来完全像...中axis.text指定的那样theme
显然我可以手动添加规范作为可选参数geom_text,但我希望它"自动"继承规范......
library("ggplot2")
ggplot(mtcars, aes(x = mpg,
y = hp,
label = row.names(mtcars))) +
geom_point() +
geom_text() +
theme_minimal(base_family = "Courier")
Run Code Online (Sandbox Code Playgroud)
我有空间线(作为一个terra::vect()对象),我想与栅格(terra::rast())相交,以便通过每条线相交的 id 来识别单元格。
作为输出,我想要一个data.frame带有列line_id和的长表单cell_id(参见下面的示例)。
我更喜欢一个terra解决方案,但我也喜欢使用sf、stars、raster等包的解决方案。
对栅格进行矢量化是一种选择,但我的现实生活数据非常大 - 我可能很容易遇到性能问题。所以我想摆脱栅格的矢量化。
我想到了 terra::intersect() , but it does not take SpatRasters as an argument. And terra::rasterizeGeom()` 不返回 ids,但计算长度等......
library("terra")
# Create example lines
v <- vect(system.file("ex/lux.shp", package="terra"))
lns <- as.lines(v)[c(1,7), "ID_1"] # filter for two lines only & only keep ID
names(lns) <- "line_id"
lns
# class : SpatVector
# geometry : lines …Run Code Online (Sandbox Code Playgroud) 问题:
我想生成一个散点图highcharter::hchart,其中y是 a factor,x 是 a date。
显然,highcharter::hchart "scatter"不接受因子变量作为 y。
有什么解决方法吗?或者"scatter"只是错误的图表类型?
(评论:我知道ggplotly这是一个不错的选择,但我实际上需要一个highcharter解决方案)
例子:
假设我想按类型生成出版物的时间表。我想要一个带有d$type(=y-axis) 和d$date(=x-axis)的散点图,并且highcharter工具提示应该显示我d$title,d$type和d$date(格式正确)。
library(stringi)
library(tidyverse)
library(highcharter)
### create example data
d <- data.frame(date = sample(seq(as.Date("2001/1/1"),
as.Date("2003/1/1"),
by = "day"),
30), # Date of publication
title = stringi::stri_rand_strings(30, 5), # Title of publication
type = rep(c("book","article","tweet"),
length.out=30)) …Run Code Online (Sandbox Code Playgroud)