我希望完成两个基于彼此的列,但它们是部分填充的.
title <- c("Mrs", "Ms", "", "Ms", "Mr", "Mr", "")
gender <- c("female", "", "male", "female", "", "Male", "female")
df <- as.data.frame(cbind(title, gender))
df
title gender
1 Mrs female
2 Ms
3 male
4 Ms female
5 Mr
6 Mr Male
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我们知道如果title=Mrs
或者Ms
,那么应该填写性别female
,如果title=Mr
那时性别应该填写为male
.另一方面,如果只填写性别female
,那么标题应该是Ms
,或者male
标题应该是Mr
.
除此之外,如何在不必事先建立关系的情况下完成部分填充的表格.请参考以下示例:
c1 <- paste(rep(letters[1:12], 4))
c2 <- paste(rep(letters[13:24], 4))
df <- as.data.frame(cbind(c1, c2), stringsAsFactors=FALSE)
#replacing 8 strings in each column …
Run Code Online (Sandbox Code Playgroud) 我很习惯在ggplot2中这样做,我很难弄清楚如何使用R基础图形指定alpha值,而plot()中的col =参数用于为分类指定颜色类型变量.
使用虹膜数据集(虽然在这种情况下,为什么我们需要更改alpha值并不是真的有意义)
data(iris)
library(ggplot2)
g <- ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point(aes(colour=Species), alpha=0.5) #desired plot
plot(iris$Sepal.Length, iris$Petal.Length, col=iris$Species) #attempt in base graphics
Run Code Online (Sandbox Code Playgroud)
使用{graphics}将另一个变量映射到alpha值怎么样?例如在ggplot2中:
g2 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point(aes(colour=Species, alpha=Petal.Width))
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!
我有一个二维六边形密度图,有许多点.我希望六边形内的计数以对数刻度显示,但我无法通过ggplot2弄清楚如何做到这一点.
这是一个简单的例子:
x <- runif(1000, 50, 100)
y <- rnorm(1000, mean = 10, sd = 8)
df <- as.data.frame(cbind(x, y))
ggplot(df, aes(x, y)) + stat_binhex()
Run Code Online (Sandbox Code Playgroud) 我制作了一系列情节(散点图)的GIF,它显示了每年的纬度和经度点,显示了物种分布和种群分布.然而,在某些年份,某些物种没有价值(即当年没有收集任何个体).这会在GIF中产生问题,因为图例应从图表到图表不变.
我希望能够在图例中手动包含或插入这些缺失的物种,相应的物种名称和颜色值(即使它们不在所示的图中).
可重复的例子(虽然我不知道是否有必要):
#Creating mock longitude and latitude poitns
a <- c("1","1")
b <- c("2","2")
c <- c("3","1")
d <- c("2","1")
e <- c("2","3")
f <- c("3","3")
x <- rbind(a,b,c,d,e,f)
#Assigning species to each observation.
sp <- c("sp1", "sp1", "sp1", "sp1", "sp1", "sp2")
year <- c(2000, 2000, 2000, 1999, 1999, 1999)
x2 <- cbind(x, sp, year)
df <- as.data.frame(x2)
#Subset the df by year, which I have to do in my original dataset.
df99 <- subset(df, year==1999)
df00 <- subset(df, …
Run Code Online (Sandbox Code Playgroud) 我不确定这是否是提出这个问题的正确位置,但有没有人建议访问可用于ggmap的不同地图样式?CloudMade不再为不是"企业帐户"的帐户提供API密钥.
从"ggmap:使用ggplot2进行空间可视化"(Kahle和Wickham),他们建议使用Stamen或Google地图,但我正在寻找与这些不同的风格.
任何人都可以建议可以用于ggmap的地图样式库吗?
干杯
我试图找到两个点之间的欧氏距离,由不规则多边形限制.(即,距离必须计算为通过窗口的路线)
这是一个可重复的例子:
library(spatstat)
#Simple example of a polygon and points.
ex.poly <- data.frame(x=c(0,5,5,2.5,0), y=c(0,0,5,2.5,5))
points <- data.frame(x=c(0.5, 2.5, 4.5), y=c(4,1,4))
bound <- owin(poly=data.frame(x=ex.poly$x, y=ex.poly$y))
test.ppp <- ppp(x=points$x, y=points$y, window=bound)
pairdist.ppp(test.ppp)#distance between every point
#The distance result from this function between point 1 and point 3, is given as 4.0
Run Code Online (Sandbox Code Playgroud)
但是我们只是从绘制点来了解
plot(test.ppp)
Run Code Online (Sandbox Code Playgroud)
路径被限制在多边形时的距离应该更大(在这种情况下,为5.00).
在{spatstat}中我是否还有其他功能可以做到这一点?或者是否有人对另一个可以执行此操作的程序包有任何其他建议?
我试图找到水体中两点之间的距离,因此实际数据中的不规则多边形更复杂.
任何帮助是极大的赞赏!
干杯
我正在尝试使用滑块来控制纵向空间数据集中的年份,基本上是一组散点图.我无法弄清楚如何将滑块分配给这个变量 - 你能用ggvis做到吗?
简化的数据集:
data <- data.frame(year=rep(2000:2002, each=23),
x=rnorm(23*3,10), y=rnorm(23*3,10),
count=c(rnorm(23,2), rnorm(23,4), rnorm(23,6)))
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
### This is what is looks like in ggplot2, I'm aiming to be able to toggle
### between these panels
ggplot(data, aes(x, y, size=count)) + geom_point() + facet_grid(~year)
### Here is where I'm at with ggvis
data %>%
ggvis(~x, ~y, size=~count) %>%
layer_points()
# I'm not sure how to assign a variable (year) to a slider, I've been trying
# within the layer_points() function
### I …
Run Code Online (Sandbox Code Playgroud) 我想在散点图中标记点,但只标记facet_zoom
面板内的点。下面是一个例子:
library(ggplot2)
library(ggforce)
library(ggrepel)
library(magrittr)
labels <- letters
example_values_x <- rnorm(26)
example_values_y <- rnorm(26)
df <- data.frame(labels,
example_values_x,
example_values_y)
df %>% ggplot(aes(y = example_values_y,
x = example_values_x)) +
geom_point() +
facet_zoom(x = example_values_x > 0.5) +
geom_label_repel(data = filter(df, example_values_x > 0.5), aes(label = labels))
Run Code Online (Sandbox Code Playgroud)
知道如何使标签也不会出现在非缩放面板上吗?
我制作了一系列人口分布图的GIF(使用ggplot2和Image Magick),其中每个样本都有个体数量以及纬度和经度值。但是,每个图的值范围不同,因此图之间的大小是恒定的。我正在尝试在图形之间保持值的大小不变(即,低于该点的值为30,则该点的大小将大于该图之前的值为10的大小)。
这是一个可重现的示例:
#simulating latitude and longitude point creation
a <- c("1","1")
b <- c("2","2")
c <- c("3","1")
x <- rbind(a,b,c)
x <- as.data.frame(x)
#simulating the total individual count in my data
mag1 <- as.numeric(c("2", "6", "10"))
mag2 <- as.numeric(c("2", "6", "30"))
d1 <- cbind(x, mag1)
d2 <- cbind(x, mag2)
#Plotting the two scatter plots
g1 <- ggplot(data=d1, aes(x=V1, y=V2)) + geom_point(aes(size=mag1))
g1
g2 <- ggplot(data=d2, aes(x=V1, y=V2)) + geom_point(aes(size=mag2))
g2
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏!(如果代码混乱,或者我的问题没有100%清楚地传达,我是R的新手,请提前道歉!)
我正在绘制一些温度数据作为深度的函数.但我希望它对非科学家更友好,并明确表示顶部是水面.有关如何做到这一点的任何想法?(艺术浪潮的奖金!)
目前为止有以下几种选择:
library(dplyr); library(ggplot2); library(magrittr);
temperature <- rnorm(30, mean = 20)
depth <- seq(0:29)
df <- data.frame(temperature, depth)
no_surface <- df %>%
ggplot(aes(y = depth, x = temperature, colour = temperature)) +
geom_path(size = 2) +
scale_y_reverse() +
scale_colour_gradient(low = "blue", high = "red")+
theme_classic() +
theme(legend.position = "none")
flat_surface <- no_surface + geom_hline(yintercept = 0)
wavy_surface <- no_surface + stat_function(fun = function(x)sin(x^1.5),
size = 1)
Run Code Online (Sandbox Code Playgroud)