我在ggplotly中使用了geom_bar图,它使负条呈正值。任何想法为什么会出现这种情况,特别是如何解决这个问题?
library(ggplot2)
library(plotly)
dat1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(-13.53, 16.81, 16.24, 17.42)
)
# Bar graph, time on x-axis, color fill grouped by sex -- use position_dodge()
g <- ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge())
ggplotly(g)
为什么第一个柱子是正方向,负值?
我使用的版本是最新版本:
plotly_3.4.13
ggplot2_2.1.0
答案 0 :(得分:3)
如果您将绘图对象写入另一个变量,则可以修改绘图属性,包括&#39;数据&#39;它用于渲染图。
对于您的具体示例,请将此附加到您的代码中:
#create plotly object to manipulate
gly<-ggplotly(g)
#confirm existing data structure/values
gly$x$data[[1]]
# see $y has values of 13.53, 16.81 which corresponds to first groups absolute values
#assign to original data
gly$x$data[[1]]$y <- dat1$total_bill[grep("Female",dat1$sex)]
#could do for second group too if needed
gly$x$data[[2]]$y <- dat1$total_bill[grep("Male",dat1$sex)]
#to see ggplotly object with changes
gly
答案 1 :(得分:0)
我已经提出了一个通用解决方案,适用于使用小平面包装的情况。以下是玩具数据问题的一个示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Resposive layout</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div id="header"> <h3> Header area </h3>
</div>
<div id="wrapper">
<div id="main1">
<div id="leftside"><h3> Categories </h3></div>
<div id="middle"><h3> Slider </h3></div>
<div id="rightside"><h3> Mobile App </h3></div>
</div>
<br>
<div id="special_discount">
<div id="sp_dis_leftside"><h3> Mega Discount box </h3></div>
<div id="sp_dis_middle"><h3> Top Brands </h3></div>
<div id="sp_dis_rightside"><h3> Crazy Discount </h3></div>
</div>
<br>
<div id="newsletter"><h3>Subscribe to Newsletter</h3></div>
<br>
<div id="recommended">
<div id="recom_leftside"><h3> Best for You </h3></div>
<div id="recom_rightside"><h3> Recommended Products Manual Slider </h3></div>
</div>
<br>
<div id="ads1"><h3>Single Product Ads to Buy</h3></div>
<br>
<div id="category1">
<div id="cat1_leftside"><h3> Horizontal Slider Cat 1 </h3></div>
<div id="cat1_rightside"><h3> </h3></div>
<div id="home_category1_boxes1">
<ol><h3>Men Dress</h3> </ol>
<ol>Men Dress 1</ol>
<ol>Men Dress 2</ol>
<ol>Men Dress 3</ol>
<br>
<ol><h3>Men Accessories</h3></ol>
<ol>Men Accessories1</ol>
<ol>Men Accessories2</ol>
<ol>Men Accessories3</ol>
</div>
<div id="home_category1_boxes2"> <img src="images/boxes/cat1/big.jpg" width="376" height="350"></div>
<div id="home_category1_boxes3">
<img src="images/boxes/cat1/box1.jpg" width="140px"/>
<img src="images/boxes/cat1/box2.jpg" width="140px"/>
<img src="images/boxes/cat1/box3.jpg" width="140px"/>
<img src="images/boxes/cat1/box4.jpg" width="140px"/>
</div>
</div>
<br>
<div id="ads_worldwide"><h3>Shopping Worldwide</h3></div>
<br>
<div id="category2">
<div id="cat2_leftside"><h3> Horizontal Slider Cat 2 </h3></div>
<div id="cat2_rightside"><h3> Category 2 Top Brans with 1 big pic and 4 box pics </h3></div>
</div>
<br>
<div id="ads2"><h3>Single Product Ads to Buy</h3></div>
<br>
<div id="category3">
<div id="cat3_leftside"><h3> Horizontal Slider Cat 3 </h3></div>
<div id="cat3_rightside"><h3> Category 3 Top Brans with 1 big pic and 4 box pics </h3></div>
</div>
<br>
<div id="category4">
<div id="cat4_leftside"><h3> Horizontal Slider Cat 4 </h3></div>
<div id="cat4_rightside"><h3> Category 4 Top Brans with 1 big pic and 4 box pics </h3></div>
</div>
<br>
<div id="ads3"><h3>Single Product Ads to Buy</h3></div>
<br>
<div id="category5">
<div id="cat5_leftside"><h3> Horizontal Slider Cat 5 </h3></div>
<div id="cat5_rightside"><h3> Category 5 Top Brans with 1 big pic and 4 box pics </h3></div>
</div>
<br>
<div id="category6">
<div id="cat6_leftside"><h3> Horizontal Slider Cat 6 </h3></div>
<div id="cat6_rightside"><h3> Category 6 Top Brans with 1 big pic and 4 box pics </h3></div>
</div>
<br>
<div id="ads4"><h3>Single Product Ads to Buy</h3></div>
<br>
<div id="category7">
<div id="cat7_leftside"><h3> Horizontal Slider Cat 7 </h3></div>
<div id="cat7_rightside"><h3> Category 7 Top Brans with 1 big pic and 4 box pics </h3></div>
</div>
<br>
<div id="category8">
<div id="cat8_leftside"><h3> Horizontal Slider Cat 8 </h3></div>
<div id="cat8_rightside"><h3> Category 8 Top Brans with 1 big pic and 4 box pics </h3></div>
</div>
<br>
<div id="ads5"><h3>Single Product Ads to Buy</h3></div>
<br>
<div id="category9">
<div id="cat9_leftside"><h3> Horizontal Slider Cat 9 </h3></div>
<div id="cat9_rightside"><h3> Category 9 Top Brans with 1 big pic and 4 box pics </h3></div>
</div>
<br>
<div id="category10">
<div id="cat10_leftside"><h3> Horizontal Slider Cat 10 </h3></div>
<div id="cat10_rightside"><h3> Category 10 Top Brans with 1 big pic and 4 box pics </h3></div>
</div>
<br>
<div id="footer">
</div>
</div>
</body>
</html>
当我们使用ggplotly时,我们会看到OP看到了什么,这些消极因素消失了:
set.seed(45)
df <- data.frame( group=rep(1:4,5), TitleX=rep(1:5,4), TitleY=sample(-5:5,20, replace = TRUE))
h <- ggplot(df) + geom_bar(aes(TitleX,TitleY),stat = 'identity') + facet_wrap(~group)
h
我编写了一个函数,它将检查每个构面列表中的实例,其中文本中的y值为0,这似乎是我正在解决的问题的共存问题:
gly <- ggplotly(h)
gly
现在我将此函数应用于每个方面的数据:
fix_bar_ly <- function(element,yname){
tmp <- as.data.frame(element[c("y","text")])
tmp <- tmp %>% mutate(
y=ifelse(grepl(paste0(yname,": 0$"),text),
ifelse(y!=0,-y,y),
y)
)
element$y <- tmp$y
element
}
由于某种原因,条形之间的空格已经消失......但至少在适当的位置值为负。