按填充变量子集的总和排列条形图

时间:2016-03-18 18:56:25

标签: r ggplot2

示例数据:

set.seed(145)

df <- data.frame(Age=sample(c(1:10),20,replace=TRUE),
                 Rank=sample(c("Extremely","Very","Slightly","Not At All"),
                             20,replace=TRUE),
                 Percent=(runif(10,0,.01)))

df.plot <- ggplot(df,aes(x=Age,y=Percent,fill=Rank))+
           geom_bar(stat="identity")+
           coord_flip()
df.plot

在ggplot中,如何通过Ranks&#34;极其&#34;的总和重新排序x=Age。和&#34;非常&#34;只要?

我尝试使用下面的内容,没有成功。

df.plot <- ggplot(df,aes(x=reorder(Age,Rank=="Extremely",sum),y=Percent,fill=Rank))+
              geom_bar(stat="identity")+
              coord_flip()
df.plot

1 个答案:

答案 0 :(得分:3)

几个笔记:

  1. 您模拟数据的方式并不排除在某些年龄段,所有类别都没有表示(这很好)的可能性,而且在某些年龄段,某些类别也是重复的。我假设你的真实数据不是这样,所以让它成为现实。另请注意,虽然类别名称表明它们应该添加,但您的模拟逻辑不会产生累加的百分比。
  2. 我这样做的方法是根据您所需的逻辑创建年龄顺序,然后将该顺序传递给package com.provider; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.testng.Assert; import org.testng.annotations.Test; public class ProApp extends BaseClass{ @Test public void setUpConnection() throws ClassNotFoundException, SQLException, FileNotFoundException, InterruptedException, IOException { String driver_DBPath = "jdbc:oracle:thin:@Host:Port:SID"; String DB_username = "*****"; String DB_password = "*****"; String Query = "select * from Table"; Connection con = DriverManager.getConnection(driver_DBPath, DB_username, DB_password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(Query); while(rs.next()){ String Email = rs.getString("CLAIM_NUMBER"); String Pwd = rs.getString("INDIVIDUAL_NUM"); testUserNamePassword(Email, Pwd); } } @Test(priority=1) public void clickLoginLink() throws InterruptedException, IOException { Properties obj = new Properties(); FileInputStream objfile = new FileInputStream(System.getProperty("user.dir") +"\\src\\com\\provider\\Object.Properties"); obj.load(objfile); Thread.sleep(1000); driver.findElement(By.xpath(obj.getProperty("ClickOnLoginLink"))).click(); Thread.sleep(1000); } @Test(priority=2) public void testUserNamePassword(String Email1, String Pwd1) throws InterruptedException, IOException { Thread.sleep(1000); Properties obj = new Properties(); FileInputStream objfile = new FileInputStream(System.getProperty("user.dir") +"\\src\\com\\provider\\Object.Properties"); obj.load(objfile); Thread.sleep(1000); driver.findElement(By.xpath(obj.getProperty("Email"))).clear(); Thread.sleep(1000); driver.findElement(By.xpath(obj.getProperty("Email"))).sendKeys(Email1); Thread.sleep(1000); driver.findElement(By.xpath(obj.getProperty("Pwd"))).clear(); Thread.sleep(1000); driver.findElement(By.xpath(obj.getProperty("Pwd"))).sendKeys(Pwd1); Thread.sleep(1000); driver.findElement(By.xpath(obj.getProperty("Submit"))).click(); Thread.sleep(1000); } @Test(priority=3) public void loginVerify() throws InterruptedException, IOException{ Properties obj = new Properties(); FileInputStream objfile = new FileInputStream(System.getProperty("user.dir") +"\\src\\com\\provider\\Object.Properties"); obj.load(objfile); Assert.assertEquals("Wel Come To Testing World!!!", driver.findElement(By.xpath(obj.getProperty("WelComeToTestingWorld"))).getText()); } @Test(priority=4) public void logonVerify() throws InterruptedException, IOException{ Properties obj = new Properties(); FileInputStream objfile = new FileInputStream(System.getProperty("user.dir") +"\\src\\com\\provider\\Object.Properties"); obj.load(objfile); WebElement DashboardHeader = driver.findElement(By.xpath(obj.getProperty("WelComeToTestingWorld"))); DashboardHeader.getText().equals("Wel Come To Testing World!!!"); } } 调用。这解耦了排序逻辑并允许任意排序逻辑。
  3. 以下是我认为您正在寻找的内容:

    factor

    哪个代码产生:

    enter image description here