我正在绘制 2013 年、2014 年、2015 年三个不同年份的时间序列。
require(quantmod)
require(ggplot2)
getSymbols("AAPL", from='2013-01-1')
aapl.df = data.frame(date=time(AAPL), coredata(AAPL.Close))
ggplot(data=aapl.df, aes(x=date, y=AAPL.Close, group=1))+geom_line()
Run Code Online (Sandbox Code Playgroud)
如何在 ggplot 中绘制收盘价,以便每年在绘图上都有不同的背景颜色图块?
我注意到 ggplot2 在 x 和 y 轴之间留下了一个小间隙。考虑以下代码:
require(ggplot2, quietly=TRUE)
axisLines = element_line(color="black", size = 2)
p= ggplot(BOD, aes(x=Time, y=demand)) + geom_line() +
theme(axis.line.x = axisLines,
axis.line.y = axisLines,
panel.background = element_blank())
p
Run Code Online (Sandbox Code Playgroud)
结果显示了图形中丑陋的“缺失角”(用红色圆圈强调)。
我还没有看到不会发生这种情况的 ggplot 示例(但是,有很多示例,例如https://rpubs.com/Koundy/71792)。
我尝试在轴上添加 geom_vline 或 geom_hline,但它们没有填补空白,因为它在图形区域之外。
如果有人对此有解决方案,我将不胜感激,例如手动添加点或稍微移动轴。
我使用分面视图绘制了 3 个直方图,并为均值和中值添加了 vlines。
我想添加一个图例来指示哪个 vline 指的是哪个统计数据。
ggplot(x, aes(x=earnw)) + geom_histogram(binwidth=100, colour="black", fill="white") +
facet_grid(tuyear ~ .) +
geom_vline(data=a, aes(xintercept=earnw.mean), linetype="dashed", size=1, color="mean") +
geom_vline(data=b, aes(xintercept=earnw.med), linetype="dashed", size=1, color="median") +
scale_color_manual(name = "statistics", values = c("mean" <- "red", "median" <- "blue")) +
labs(title="Histogram for Weekly Earnings of Respondents") +
labs(x="Weekly Earnings of Respondents", y="Count") +
scale_x_continuous(breaks=seq(0,3000,200),lim=c(0,3000))
Run Code Online (Sandbox Code Playgroud)
以下代码返回错误:
grDevices::col2rgb(colour, TRUE) 中的错误:颜色名称“mean”无效
我正在关注以下备忘单:
https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf
并尝试使用以下代码制作一些列表(从备忘单中粘贴的副本)
---
title: "Test"
author: "Test"
date: "7 August 2017"
output: html_document # or pdf_document
---
* unordered list
* item 2
+ sub-item 1
+ sub-item 2
1. ordered list
2. item 2
+ sub-item 1
+ sub-item 2
Run Code Online (Sandbox Code Playgroud)
但结果与备忘单中的结果不同,圆圈不相同,子项目不会缩进.
我有一个带有分类 x 轴的数据框,称为类别,y 轴是丰度,按序列着色。对于每个类别,我试图按丰度对堆栈进行重新排序,以便很容易地可视化哪个序列在底部的比例最高,在顶部的比例最低。
目前,我可以制作这样的条形图:
s<-"Sequence Abundance Category
CAGTG 0.8 A
CAGTG 0.2 B
CAGTG 0.6 C
CAGTG 0.3 D
CAGTG 0.1 E
GGGAC 0.1 A
GGGAC 0.1 B
GGGAC 0.3 C
GGGAC 0.6 D
GGGAC 0.1 E
CTTGA 0.1 A
CTTGA 0.7 B
CTTGA 0.1 C
CTTGA 0.1 D
CTTGA 0.8 E"
d<-read.delim(textConnection(s),header=T,sep=" ")
g = ggplot(d,aes(x = Category, y = Abundance, fill = Sequence)) +
geom_bar(position = "fill",stat = "identity")
Run Code Online (Sandbox Code Playgroud)
我的数据与此非常相似:Ordering stacks by size in a ggplot2 …
我正在使用素食库制作一些绘图,使用以下代码:
raremax <- min(colSums(mydata))
col <- palette()
lty <- c("solid", "dashed", "longdash", "dotdash")
pars <- expand.grid(col = col, lty = lty, stringsAsFactors = FALSE)
out <- with(pars[1:18, ], rarecurve(mydata, step = 100, sample = raremax,
cex =0.6, ylab="OTUs", label=F, col=col, lty=lty, lwd=2))
Run Code Online (Sandbox Code Playgroud)
然后我使用以下代码添加一个图例:
legend("bottomright", names(mydata), col=pars[1:18,1], lty= pars[1:18,2],
lwd=2, cex=0.5, xjust=1, ncol=2, x.intersp=0.5, y.intersp=0.5, bg="white")
Run Code Online (Sandbox Code Playgroud)
我想减少图例列之间的空间,同时减少图例框的大小,但我找不到办法做到这一点。
任何人都可以为我提供一些帮助吗?
我制作了一个DNN回归模型来预测我们在数据表中没有的结果,但我不能制作张量板.
此代码来自https://deeplearning4j.org/linear-regression.html 以及香港大学Sunghun Kim撰写的讲义.
import tensorflow as tf
import numpy as np
tf.set_random_seed(777) #for reproducibility
# data Import
xy = np.loadtxt('Training_Data.csv', delimiter=',', dtype=np.float32)
x_data = xy[:,0:-1]
y_data = xy[:,[-1]]
# Make sure the shape and data are OK
print(x_data.shape, x_data)
print(y_data.shape, y_data)
# input place holders
X = tf.placeholder(tf.float32, shape=[None, 2])
Y = tf.placeholder(tf.float32, shape=[None, 1])
# weight & bias for nn Layers
W1 = tf.get_variable("W1", shape=[2, 512],initializer=tf.contrib.layers.xavier_initializer())
b1 = tf.Variable(tf.random_normal([512]))
L1 = tf.nn.relu(tf.matmul(X, W1) + b1)
W2 = …
Run Code Online (Sandbox Code Playgroud) 我尝试计算期望值200和标准偏差20的正态分布密度的积分。从-Inf到Inf,这应该是1。
我得到以下内容:
> integrate(dnorm, mean=200, sd=20,-Inf, Inf)$value
[1] 1.429508e-08
Run Code Online (Sandbox Code Playgroud)
对于低于169的期望值,我得到正确的值,1.对于更大的期望值,如何获得正确的值?
我绘制了瑞士的区域(多边形shapefile)并通过坐标添加了点(瑞士气象站)。
# Boundaries with data-points plotted
library(rgdal)
library(readxl)
library(sp)
library(ggplot2)
library(maptools)
library(plyr)
library(raster)
# import swiss country frontiers (.shp file)
gb <- readOGR("swissBOUNDARIES3D_1_3_TLM_KANTONSGEBIET.shp")
# import coordinates of weather stations from excel file (.xlsx)
coord <- read_excel("SMN-Stationen_20151222.xlsx")
head(coord)
# A tibble: 6 x 10
SINCE_DT NAT_IND_TX NAT_ABBR_TX NAME_TX X_KM_COORD_NU Y_KM_COORD_NU HEIGHT_ASL_NU
<dttm> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 2015-12-15 0600 ARO Arosa 771030 184826 1878
2 2015-10-27 3420 LAC Lachen / Galgenen 707637 226334 468
3 2015-09-08 8040 VEV Vevey …
Run Code Online (Sandbox Code Playgroud) 我想为 ggplot 添加置信区间误差线。
我有一个数据集,我用 ggplot 将其绘制为:
df <- data.frame(
Sample=c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5"),
Weight=c(10.5, NA, 4.9, 7.8, 6.9))
p <- ggplot(data=df, aes(x=Sample, y=Weight)) +
geom_bar(stat="identity", fill="black") +
scale_y_continuous(expand = c(0,0), limits = c(0, 8)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)
p
Run Code Online (Sandbox Code Playgroud)
我是添加误差线的新手。我使用 geom_bar 查看了一些选项,但无法使其工作。
我将不胜感激任何帮助将置信区间误差线放入条形图中。谢谢你!