简体   繁体   English

使用ggplot2制作barplot时如何添加辅助轴

[英]How to add secondary axis while making barplot using ggplot2

I have a dataset of a type as shown 我有一个如图所示类型的数据集

Seasons        A        B        C        A1        B1        C1  
Winter         97       94       87       0.2       0.4       0.3  
Summer         92       94       101      1         0.7       0.3

There are values for each season (Summer, Winter, autumn, spring) and with variables from (A to E) and (A1 to E1). 每个季节都有值(夏季,冬季,秋季,春季),变量的范围是(A到E)和(A1到E1)。 While drawing a barplot using ggplot2, the bar height of A1 to E1 is very less due to their low values and I wish to move them to the secondary axis but I don't know how to do that. 使用ggplot2绘制条形图时,A1到E1的条形高度由于其较低的值而非常小,我希望将它们移到辅助轴,但是我不知道该怎么做。 Please suggest the code. 请建议代码。 I am sharing my code until now. 到目前为止,我一直在共享我的代码。

library(readxl)
library(ggplot2)
library(readxl)
cell_viability_data <- read_excel("C:/Users/CEZ178522/Downloads/ananya/Cell_viability.xlsx")
cell_viability_data


plot1 <- ggplot(data=cell_viability_data, aes(x=Seasons, y= CellViability, fill= Types)) +
  geom_bar(stat="identity", position=position_dodge()) +
             labs(title = "Seasonal Cell Viability") +
             theme(axis.text.x = element_text(colour = "grey1", size = 10),
                   axis.text.y = element_text(colour = "grey1", size = 10),
                   plot.title = element_text(hjust = 0.5))
plot1

在此处输入图片说明

I need the small bars to move to secondary axis 我需要小棒移动到副轴

Secondary y-axes were for a long time banned in ggplot because they usually do more damage than good. ggplot长时间禁止使用次级y轴,因为它们通常造成的损害大于利弊。 The only option for now is to display an auxiliary, secondary y-axis which has a direct, proportional transformation from the primary y-axis. 现在唯一的选择是显示辅助的辅助y轴,该辅助y轴与主要y轴具有直接的比例转换。 In other words, the secondary y-axis is a supplemental axis which displays the same information , but on a different scale (thing Celcius and Fahrenheit). 换句话说,次要y轴是一个补充轴,它显示相同的信息 ,但显示比例不同(摄氏和华氏温度)。

What you are asking is to have a subset of data points inflated by some arbitrary value , so they are "on par" with the remaining. 您要问的是让某个数据点的子集膨胀一些任意值 ,因此它们与其余部分“同等”。 Consider this: Can you, by choice of scaling constant, make values A1-E1 appear much higher than values AE? 考虑一下:通过选择缩放常数,可以使值A1-E1看起来比值AE高吗? Can you, by choice of scaling constant, make values A1-E1 appear much, much lower than values AE? 通过选择缩放常数,可以使值A1-E1看起来远小于值AE吗? Can you, by choice of scaling constant, make values A1-E1 be "on par" with AE, but always slighter lower? 您可以通过选择缩放常数来使A1-E1值与AE处于“同等水平”,但始终略低一些吗? If so, to any question, your data visualisation cannot be trusted. 如果是这样,则无论如何,您的数据可视化均不可信任。

Consider instead: What is the important comparison you are trying to make? 相反,请考虑:您要进行的重要比较是什么? Season-to-season for each type? 每种季节都不同? A vs. A1? A与A1? Take out a pen and paper, and try to sketch what you want to compare, and what issues you are encountering when making a comparison. 拿出笔和纸,尝试勾勒出您想要比较的东西,以及进行比较时遇到的问题。 Then you are ready to make the visualisation in R/ggplot. 然后,您就可以在R / ggplot中进行可视化了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM