如何在ggplot2中绘制Gamma分布

时间:2017-07-19 21:20:16

标签: r ggplot2 gamma-distribution

我在R中有一个我试图在ggplot2中复制的情节。我有以下代码:

theta = seq(0,1,length=500)
post <- dgamma(theta,0.5, 1)
plot(theta, post, type = "l", xlab = expression(theta), ylab="density", lty=1, lwd=3)

enter image description here

我试图在ggplot2中复制这个情节,这是我能够得到的最接近的。

df=data_frame(post,theta)
ggplot(data=df,aes(x=theta))+
  stat_function(fun=dgamma, args=list(shape=1, scale=.5))

enter image description here

1 个答案:

答案 0 :(得分:4)

您未正确匹配参数。 dgamma(theta, 0.5, 1) 的签名是

dgamma(theta, shape=0.5, rate=1)

所以当你打电话时

ggplot

这是

ggplot(data=df,aes(x=theta))+
  stat_function(fun=dgamma, args=list(shape=0.5, rate=1))

表示您将scale_y_continuous(limits=c(0,12))翻译为

public class task {

public static void main(String[] args)  {
    System.setProperty("webdriver.gecko.driver", "g://geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.cheapoair.com/");
    driver.manage().deleteAllCookies();

    driver.findElement(By.xpath(".//*[@id='ember746']")).sendKeys("DFW");
    driver.findElement(By.xpath(".//*[@id='ember751']")).sendKeys("JFK");
    driver.findElement(By.xpath(".//*[@id='owFlight']")).click();

    driver.findElement(By.xpath(".//*[@id='departCalendar_0']")).click();
    driver.findElement(By.xpath(".//*
    [@id='calendarCompId']/section/div/div[1]/ol/div[26]/li")).click();
    driver.findElement(By.xpath(".//*[@id='ember751']")).sendKeys("JFK");
    driver.findElement(By.xpath(".//*[@id='owFlight']")).click();

    driver.findElement(By.xpath(".//*
    [@id='ember730']/section/form/input")).click();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);



    //WebElement target = driver.findElement(By.xpath(".//*
     [@id='DivDepart']/div/div/div[1]/div[2]"));
    //WebElement source = driver.findElement(By.xpath(".//*
    [@id='DivDepart']/div/div/div[1]/div[1]"));
    //a.dragAndDrop(source, target).build().perform();

      WebElement slider = driver.findElement(By.xpath(".//*
      [@id='DivDepart']/div/div/div[1]/div[2]"));
       //WebElement slider = driver.findElement(By.id("DivDepart"));
     Actions a = new Actions(driver);


      //a.dragAndDropBy(slider, 30, 0).build().perform();


      //a.clickAndHold(slider).moveByOffset(30, 
       0).release(slider).build().perform();
            //System.out.println("moved");
                   //JavascriptExecutor js = (JavascriptExecutor)driver;
        org.openqa.selenium.interactions.Action dragAndDrop =

           a.clickAndHold(slider).moveByOffset(40,0).release().build();

          dragAndDrop.perform();


           //js.executeScript("window.scrollBy(200,0)");

             }

             }

如果您愿意使用# Space Invaders import random import pygame from os import path image_directory = path.join(path.dirname(__file__), "Images") sound_directory = path.join(path.dirname(__file__), "Sounds") WIDTH = 800 HEIGHT = 600 FPS = 60 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.transform.scale(player_image, (50, 38)) self.image.set_colorkey(BLACK) self.rect = self.image.get_rect() self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT - 20 self.speedx = 0 self.shoot_delay = 250 self.last_shot_time = pygame.time.get_ticks() def update(self): self.speedx = 0 keystate = pygame.key.get_pressed() if (keystate[pygame.K_LEFT] or keystate[pygame.K_a]) and self.alive(): self.speedx = -5 if (keystate[pygame.K_RIGHT] or keystate[pygame.K_d]) and self.alive(): self.speedx = 5 if keystate[pygame.K_SPACE] and player.alive(): self.shoot() self.rect.x += self.speedx if self.rect.right > WIDTH: self.rect.right = WIDTH if self.rect.left < 0: self.rect.left = 0 def shoot(self): now = pygame.time.get_ticks() if now - self.last_shot_time > self.shoot_delay: self.last_shot_time = now bullet = Bullet(self.rect.centerx, self.rect.top) all_sprites.add(bullet) bullets.add(bullet) shoot_sound.play() class Bullet(pygame.sprite.Sprite): def __init__(self, x_spawn, y_spawn): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((5, 25)) self.image.fill(WHITE) self.image.set_colorkey(BLACK) self.rect = self.image.get_rect() self.rect.bottom = y_spawn self.rect.centerx = x_spawn self.y_speed = -10 def update(self): self.rect.y += self.y_speed if self.rect.bottom < 0: self.kill() class Mob(pygame.sprite.Sprite): def __init__(self, x_spawn, y_spawn): pygame.sprite.Sprite.__init__(self) if y_spawn == initial_y_spawn: self.image = pygame.transform.scale(top_row_image, (45, 35)) self.score = 100 if y_spawn == initial_y_spawn + 50 or y_spawn == initial_y_spawn + 100: self.image = pygame.transform.scale(second_third_row_image, (45, 35)) self.score = 50 if y_spawn == initial_y_spawn + 150 or y_spawn == initial_y_spawn + 200: self.image = pygame.transform.scale(fourth_fifth_row_image, (45, 35)) self.score = 10 self.image.set_colorkey(BLACK) self.rect = self.image.get_rect() self.rect.centerx = x_spawn self.rect.centery = y_spawn self.x_movement = 10 self.time_last_updated = pygame.time.get_ticks() self.direction = "right" def update(self): current_time = pygame.time.get_ticks() if current_time - self.time_last_updated > 250 and self.direction == "right": self.rect.centerx += self.x_movement self.time_last_updated = current_time if current_time - self.time_last_updated > 250 and self.direction == "left": self.rect.centerx -= self.x_movement self.time_last_updated = current_time def new_top_row_mob(x_spawn, y_spawn): _mob = Mob(x_spawn, y_spawn) mobs.add(_mob) def new_second_row_mob(x_spawn, y_spawn): _mob = Mob(x_spawn, y_spawn) mobs.add(_mob) def new_third_row_mob(x_spawn, y_spawn): _mob = Mob(x_spawn, y_spawn) mobs.add(_mob) def new_fourth_row_mob(x_spawn, y_spawn): _mob = Mob(x_spawn, y_spawn) mobs.add(_mob) def new_fifth_row_mob(x_spawn, y_spawn): _mob = Mob(x_spawn, y_spawn) mobs.add(_mob) ''' initialize pygame and sound ''' pygame.init() pygame.mixer.init() ''' create display and clock ''' game_display = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Space Invaders") clock = pygame.time.Clock() ''' image loading ''' background = pygame.image.load(path.join(image_directory, "maxresdefault.jpg")).convert() background_rect = background.get_rect() player_image = pygame.image.load(path.join(image_directory, "player_ship.jpg")).convert() top_row_image = pygame.image.load(path.join(image_directory, "top.jpg")).convert() second_third_row_image = pygame.image.load(path.join(image_directory, "second.jpg")).convert() fourth_fifth_row_image = pygame.image.load(path.join(image_directory, "third.jpg")).convert() ''' sound loading ''' explosion_sounds = [] for sound in ["Explosion4.wav", "Explosion8.wav"]: explosion_sounds.append(pygame.mixer.Sound(path.join(sound_directory, sound))) shoot_sound = pygame.mixer.Sound(path.join(sound_directory, "Laser_Shoot3.wav")) ''' create and add sprites to groups ''' all_sprites = pygame.sprite.Group() bullets = pygame.sprite.Group() mobs = pygame.sprite.Group() player = Player() initial_x_spawn = 50 initial_y_spawn = 75 for i in range(10): new_top_row_mob(initial_x_spawn, initial_y_spawn) new_second_row_mob(initial_x_spawn, initial_y_spawn + 50) new_third_row_mob(initial_x_spawn, initial_y_spawn + 100) new_fourth_row_mob(initial_x_spawn, initial_y_spawn + 150) new_fifth_row_mob(initial_x_spawn, initial_y_spawn + 200) initial_x_spawn += 75 all_sprites.add(player) all_sprites.add(bullets) all_sprites.add(mobs) ''' Runs the game loop ''' running = True score = 0 edge = False while running: # process input/ events for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False ''' update sprites and game ''' all_sprites.update() for alien in mobs.sprites(): if alien.rect.right >= WIDTH: for mob in mobs.sprites(): mob.direction = "left" mob.rect.bottom += 15 if alien.rect.left <= 0: for mob in mobs.sprites(): mob.direction = "right" mob.rect.bottom += 15 bullet_hits = pygame.sprite.groupcollide(mobs, bullets, True, True) for hit in bullet_hits: score += hit.score random.choice(explosion_sounds).play() ''' Draw/Render Graphics ''' game_display.blit(background, background_rect) all_sprites.draw(game_display) # keep this running at the right speed clock.tick(FPS) # after drawing everything, flip the display pygame.display.update() pygame.quit() quit() 或类似内容,也可以调整y限制。