构建条形图以比较两组数据的最佳方法是什么?
例如数据集:
Number <- c(1,2,3,4)
Yresult <- c(1233,223,2223,4455)
Xresult <- c(1223,334,4421,0)
nyx <- data.frame(Number, Yresult, Xresult)
Run Code Online (Sandbox Code Playgroud)
我想要的是横跨X的数字和彼此相邻的条形,代表各个X和Y值
我试图在同一个情节上绘制两个回归.因此,在第一次回归结束的地方,第二次回归应该继续,并且由于第二次回归线具有不同的斜率,因此产生扭结.
我是ggplot的新手,我收到了错误:"错误:美学必须是长度为1或与dataProblems相同的长度:gdpless15"
这是R文件的链接以及此所需的数据集. 数据集和代码
我基本上对变量gdp做了ols回归.变量gdp被子集化为<15000和> 15000两部分.然后我想绘制<15000的回归线,当它达到> 15000时,它将在同一图上绘制第二次回归的回归线.
我认为使用ggplot2将两个回归图一起绘制最困难.

这是我认为最接近我想要的代码的版本.
library(foreign)
library(ggplot2)
gdpless15 <- isspmacro$gdp[(isspmacro$gdp < 15000) ] <- subset(isspmacro,gdpless15)
gdpless15 <-gdp < 15000
gdpmore15 <- gdp > 15000
combinedreg <- ggplot() +
geom_point(data=less15, aes(x=gdpless15, y=hap)) +
geom_smooth(data=less15, aes(x=gdpless, y=hap), fill="blue",colour="darkblue", size=1) +
geom_point(data=more15, aes(x=gdpmore15, y=hap)) +
geom_smooth(data=more15, fill="red",colour="red", size=1)
combinedreg <- ggplot() +
geom_point(data=less15, aes(x=gdpless15, y=hap)) +
geom_smooth(data=less15, aes(x=gdpless, y=hap), fill="blue",colour="darkblue", size=1) +
geom_point(data=more15, aes(x=gdpmore15, y=hap))+
geom_smooth(method=lm) + geom_point(shape=1)
plot(combinedreg)
Run Code Online (Sandbox Code Playgroud) 我的数据看起来像
language tone count tone_percent label_pos pos
1 c positive 3460 36.16977 18.08488 7
2 c neutral 2046 21.38825 46.86389 7
3 c negative 4060 42.44198 78.77901 7
4 c# positive 3732 41.26949 20.63475 3
5 c# neutral 1832 20.25876 51.39887 3
6 c# negative 3479 38.47175 80.76413 3
7 c++ positive 3136 33.13960 16.56980 8
8 c++ neutral 2008 21.21949 43.74934 8
9 c++ negative 4319 45.64092 77.17954 8
Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用 ggplot2 条形图将它们可视化:
p <-ggplot() + theme_bw() + geom_bar(aes(y=tone_percent, x=reorder(language, …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个由书名和书籍作者组成的标签.我想在标签中强调标题,但不是作者.
这是MWE数据:
Title,Author,Pages,Date Started,Date Finished
underline('Time Travel'),'James Gleick',353,1/1/17,1/27/17
underline('The Road'),'Cormac McCarthy',324,1/28/17,3/10/17
Run Code Online (Sandbox Code Playgroud)
此代码有效,但不允许标题和作者
library(ggplot2)
library(tidyverse)
library(ggrepel)
library(ggalt)
books.2017 <- read_csv('books_2017.csv')
books.2017$`Date Started` <- as.Date(books.2017$`Date Started`, "%m/%d/%y")
books.2017$`Date Finished` <- as.Date(books.2017$`Date Finished`, "%m/%d/%y")
ggplot(books.2017, aes(x=`Date Started`, xend=`Date Finished`)) +
geom_dumbbell(aes(size=Pages),size_x=0, size_xend=0) +
geom_text_repel(aes(label=paste(Title)), parse=TRUE)
Run Code Online (Sandbox Code Playgroud)
当我尝试改为geom_text_repel:
geom_text_repel(aes(label=paste(Title,Author)), parse=TRUE)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Error in parse(text = as.character(lab)) :
<text>:1:26: unexpected string constant
1: underline('Time Travel') 'James Gleick'
^
Run Code Online (Sandbox Code Playgroud)
编辑标签看起来应该是这样的
我需要使用 ggplot 绘制不同日期的每小时数据,这是我的数据集:
数据由每小时观察组成,我想将每天的观察绘制成单独的一行。
这是我的代码
xbj1 = bj[c(1:24),c(1,6)]
Run Code Online (Sandbox Code Playgroud)
xbj2 = bj[c(24:47),c(1,6)]
xbj3 = bj[c(48:71),c(1,6)]
ggplot()+
geom_line(data = xbj1,aes(x = Date, y= Value), colour="blue") +
geom_line(data = xbj2,aes(x = Date, y= Value), colour = "grey") +
geom_line(data = xbj3,aes(x = Date, y= Value), colour = "green") +
xlab('Hour') +
ylab('PM2.5')
Run Code Online (Sandbox Code Playgroud)
请就此提出建议。
我有一个数据框dratiodf:
Date D10 D21 D63 D126 D252
2019-09-23 0.1557585 0.3977892 0.8583822 0.7153107 0.7517688
2019-09-24 0.1513844 0.2142586 0.7501128 0.6736790 0.7275896
2019-09-25 0.5314460 0.4254800 0.8604258 0.6612713 0.7469207
2019-09-26 0.5135381 0.4250006 0.9424716 0.7008503 0.7661933
2019-09-27 0.4816461 0.2371428 0.7969672 0.6351504 0.7307106
2019-09-30 0.6414031 0.3407633 0.8207621 0.6854996 0.7346074
Run Code Online (Sandbox Code Playgroud)
我想做的是将这些列融合在一起,形成一个如下所示的数据框:
Date: Type: Value:
2019-09-23 D10 0.1557585
2019-09-23 D21 0.3977892
2019-09-23 D63 0.8583822
2019-09-23 D126 0.7153107
2019-09-23 D252 0.7517688
2019-09-34 D10 0.1513844
2019-09-34 D21 0.2142586
Run Code Online (Sandbox Code Playgroud)
我想要这个,以便我可以按类型划分最终的绘图,如下所示:
ggplot(dratiodf, aes(x=Date, y=Value))+
geom_line()+
facet_wrap(.~type)+
theme_wsj()
Run Code Online (Sandbox Code Playgroud)
我尝试过使用熔化功能,但我就是不知道如何使用它。
另外,你能发现我的图形代码有什么问题吗?
我想这样做是因为它对于在 R 中制作“绘图”很有用。
我想看到什么,例如:
我有一个来自“boshek”的解决方案ggimage,但它有点有限。
我试着用plotly.
no_axis <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = FALSE,
showgrid = FALSE
)
plot_ly() %>%
layout(title = 'Quantity of cars in West Europe countries', xaxis = no_axes, yaxis = no_axes,
images = list(
source = "https://jeroen.github.io/images/superfrink.gif",
x = 1, y = 0.5,
sizex = 3, sizey = 2,
xref = "x", yref = "y",
xanchor = "left", yanchor = "bottom"
)
)
Run Code Online (Sandbox Code Playgroud)
我只能添加一张图片,无法添加带有文本的标签。
我正在使用 ggplot 绘制南美洲平均气温的偏差图表。
为了绘制此图,我使用以下代码:
ggplot(bias.df2) +
geom_tile(aes(x=x, y=y, fill=pr), alpha=0.8) +
scale_fill_viridis(na.value="white") +
coord_equal() +
theme(legend.position="bottom") +
theme(legend.key.width=unit(2, "cm"))+
scale_color_continuous(limits=c(-10,10),breaks=brkbias)
Run Code Online (Sandbox Code Playgroud)
生成以下颜色条:
但我需要颜色条看起来像这样:
我尝试修改颜色条的限制,但似乎没有任何东西可以生成我需要的那种绘图。非常感谢任何帮助解决这个问题的帮助。
编辑:我正在使用的数据可以在此 csv文件中找到。
我想将度数符号 (\xc2\xb0) 添加到 y 轴上的标签(而不是轴标题)。我在该轴上有一个排名,数字范围为 1-11,我希望每个人都说“1\xc2\xb0”,而不仅仅是“1”。有没有办法做到这一点?
\n